0
我需要列出目錄中的所有項目並列出所有子目錄中的所有項目。我有這個功能:遞歸列出Linux中C上的目錄
void rls_handler(const char *name, int indent){
DIR *dir;
struct dirent *sd;
dir = opendir(name);
while((sd = readdir(dir)) != NULL){
if(sd->d_type == DT_DIR){ //if item is a directory, print its contents
char path[1024];
if((strcmp(sd->d_name, ".")) !=0 && (strcmp(sd->d_name, "..")) != 0){ //skip '.' and '..'
printf("%*s[%s]\n",indent,"",sd->d_name);
rls_handler(path,indent+2); //recurse through rls_handler with subdirectory & increase indentation
}else{
continue;
}
}else{
printf("%*s- %s\n",indent, "", sd->d_name);
}
}//end while
closedir(dir);
}//end rls_handler
我得到一個segfault在行:while((sd = readdir(dir)) != NULL)
。任何人都可以幫我弄清楚爲什麼我得到這個錯誤?
檢查只是段錯誤之前,什麼是在'dir'。 – Neo
'rls_handler(path,indent + 2);' '路徑'使用未初始化。 –