我想编写一个程序,给定文件名后,在当前目录及其子目录中搜索该文件,并输出包含该文件的子目录名称。但是我的代码只会输出子目录名称。
#include <dirent.h>
#include <stdio.h>
#include <string.h>
void listDir(char *path, char *name)
{
DIR *dir;
struct dirent *ent;
if ((dir = opendir(path)) != NULL) {
while (( ent = readdir(dir)) != NULL) {
if (ent->d_type == DT_REG && name == ent->d_name) {
printf("%s\n", ent->d_name);
}
if (ent->d_type == DT_DIR && strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0) {
printf("%s\n", ent->d_name);
listDir("/home/dir1/ent->d_name", name);
}
}
closedir(dir);
}
}
void main()
{
listDir("/home/dir1", "file1");
}
strcmp(ent->$
似乎有些东西丢失了。 - MikeCATman nftw
.... - William Pursell