Linux C语言 遍历目录搜索文件的C源程序,支持嵌套目录搜索。
Linux下遍历目录搜索文件源程序以及执行文件,支持嵌套目录,输出目录、子目录和对应的子目录中的文件列表,以及总文件数目。 LINUX下历遍目录的方法一般是这样的:打开目录-》读取-》关闭目录 相关函数是opendir -> readdir -> closedir 这样是不能遍历目录中的所有文件。 //LINUX下目录遍历搜索文件程序,支持嵌套目录 #include <stdio.h> #include <string.h> #include <sys/types.h> #include <dirent.h> #include <sys/stat.h> int do_search_dir(char *path); int do_check_dir(char *fullpath, char* truefullpath); void usage(char *apps); int count = 0; int main(int argc,char **argv) { char fullpath[1024]={0}; if(argc != 2) { usage(argv[0]); return -1; } if( -1 ==do_check_dir(argv[1], fullpath) ) return -1; do_search_dir(fullpath); printf("\nThe total number of files is %d in the directory [%s].\n\n", count , fullpath); return 0; } 。。。。。。 。。。。。。 。。。。。