Linux下的打印当前文件夹下文件的程序
2010-11-16 11:21
477 查看
#include <limits.h> #include <stdio.h> #include <unistd.h> #include <sys/stat.h> #include <dirent.h> #include <limits.h> int list(char* filename) { struct stat statbuf; struct dirent *dirp; DIR *dp; static int depth = 0; int count = 0; if (stat(filename, &statbuf) < 0) //得到文件信息 { puts(filename); perror("fstat error"); exit(1); } if (S_ISDIR(statbuf.st_mode) == 0) //是否是目录 { //是文件 count++; for(int i = 0; i < depth; i++) { putchar('/t'); } puts(filename); } else { //是目录 for(int i = 0; i < depth; i++) { putchar('/t'); } puts(filename); if((dp = opendir(filename)) == NULL) { perror("opendir error"); exit(1); } if(chdir(filename) < 0) //更改当前目录为filename { perror("fchdir error"); exit(1); } depth++; while ((dirp = readdir(dp)) != NULL) //遍历当前文件夹 { if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0) continue; strcpy(filename, dirp->d_name); count += list(filename); } if(chdir("..") < 0) //更改当前目录为上一级目录 { perror("fchdir error"); exit(1); } depth--; } return count; } int main(int argc, char* argv[]) { char cwd_buff[PATH_MAX]; getcwd(cwd_buff, PATH_MAX); int n = list(cwd_buff); printf("total file: %i/n", n); }
Linux下的打印当前文件夹下文件的程序
相关文章推荐
- Java在linux上只能在程序的当前目录下创建文件/文件夹导致的问题
- linux 下查看有当前文件夹有多少个文件
- Linux查看当前目录下文件夹和文件的大小
- linux 下查看有当前文件夹有多少个文件
- linux查看当前文件和文件夹大小
- Linux命令行下统计当前文件夹下的文件个数
- 在linux下统计当前文件夹下的文件数
- Linux 统计当前文件夹、文件的数目
- 编写一个Python程序,能在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径
- linux 打包当前文件夹下所有文件的方法
- Linux下统计当前文件夹下的文件个数、目录个数
- linux下统计当前文件夹中文件的数目以及如何查看文件夹的大小
- linux打印缓存文件夹var/spool/cups下的文件打印完会自动删除。。。
- Linux中用find命令查找当前文件夹下的.elf文件
- linux 系统获得当前文件夹下存在的所有文件 scandir函数和struct dirent **namelist结构体
- linux eclipse打开当前文件所在文件夹
- Windows和Linux下枚举(遍历)当前文件夹下所有文件完整路径的命令行
- Linux 下统计当前文件夹下的文件个数、目录个数
- linux 打包当前文件夹下所有文件
- Linux下统计当前文件夹下的文件个数、目录个数