扫描目录以查找c中的文件

我正在尝试在c中创建一个函数,它扫描我的所有路径C:\ temp(Windows)以搜索我传递的文件(例如test.txt),并且每次找到一个返回路径以执行另一个函数在这个文件的底部写一些东西。 我设法做了写入文件的function,但无法弄清楚如何扫描文件夹并传递找到的文件的地址。

#include  #include  #include  #include  #include  #include  void printdir(char *dir, int depth) { DIR *dp; struct dirent *entry; struct stat statbuf; if((dp = opendir(dir)) == NULL) { fprintf(stderr,"cannot open directory: %s\n", dir); return; } chdir(dir); while((entry = readdir(dp)) != NULL) { lstat(entry->d_name,&statbuf); if(S_ISDIR(statbuf.st_mode)) { /* Found a directory, but ignore . and .. */ if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0) continue; printf("%*s%s/\n",depth,"",entry->d_name); /* Recurse at a new indent level */ printdir(entry->d_name,depth+4); } else printf("%*s%s\n",depth,"",entry->d_name); } chdir(".."); closedir(dp); } int main() { printf("Directory scan of /home:\n"); printdir("/home",0); printf("done.\n"); exit(0); } 

使用FindFirstFile函数。 这是使用此function的一个很好的例子。