我一直在嘗試在C上實現類似ls -R的問題,事情是我需要列表遞歸列出從給定目錄開始的所有內容,然後用列表中的那些常規文件進行操作。 這是我到目前爲止有:遞歸列表
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'不是一個完整的路徑。所以除非你當前的目錄是包含那個文件/目錄的目錄,否則你不能「統計」它。 'opendir'不會改變你的當前目錄。調用'stat'之前需要調用'chdir'。或者構建完整的路徑名稱。 – kaylum
您還需要過濾'.'和'..'條目。否則,您將以infinete遞歸結束。 – kaylum
這解決了無限遞歸問題,但我不能讓它工作,是不是我建立了完整的路徑已經與此? 'char * buf = malloc(strlen(path)+ strlen(filei-> d_name)+2);' 'strcpy(buf,path);' 'strcat(buf,「/」);' 'strcat (buf,filei-> d_name);' – Stieg