【发布时间】:2016-10-21 06:25:58
【问题描述】:
我在尝试在 C 上实现 ls -R 之类的东西时遇到了问题,问题是我需要 list 递归地列出从给定目录开始的所有内容,然后对从列表中获得的常规文件进行处理。 这是我目前所拥有的:
void ls(char* path){
DIR *directory;
struct dirent *filei;
struct stat stats;
directory = opendir(path);
if (directory != NULL)
{
while ((filei=readdir(directory))!=NULL){
stat(filei->d_name, &stats);
printf(" %s\n", filei->d_name);
if (S_ISDIR(stats.st_mode)){
char *buf = malloc(strlen(path)+strlen(filei->d_name)+2);
strcpy(buf,path);
strcat(buf,"/");
strcat(buf,filei->d_name);
ls(buf);
}
}
closedir(directory);
}
else{
printf("Error.\n");
}
}
它根本不起作用,它显示的文件甚至不在我正在使用的文件夹中。 有什么想法吗? 谢谢。
【问题讨论】:
-
这是使用文件/目录操作时的常见问题。
d_name不是完整路径。因此,除非您的当前目录是包含该文件/目录的目录,否则您不能stat它。opendir不会更改您的当前目录。在调用stat之前需要调用chdir。或者构造一个完整的路径名。 -
您还需要过滤掉
.和..条目。否则你会得到无限递归。 -
这解决了无限递归问题,但我还不能让它工作,我不是已经用这个构建了完整的路径吗? ` char *buf = malloc(strlen(path)+strlen(filei->d_name)+2);` ` strcpy(buf,path);`
strcat(buf,"/");strcat(buf,filei->d_name); -
在您发布的代码中,您正确地创建了完整的路径名,但还不足以让 stat() 使用它——请参阅我对代码的修改