我是C新手,嘗試通過當前工作目錄中的所有目錄/文件遞歸併輸出其信息。我遇到的問題是,我想不出一個好的解決方法,那就是當同一個目錄中存在兩個文件夾時,路徑第二次出現錯誤。例如,如果dir1和dir2在完成「/ something/dir1」之後處於相同路徑中,則路徑應該變爲「/ something/dir2」,但由於我寫入內容的方式而變爲「/ something/dir1/dir2」。我以爲只是跟蹤以前的路徑,但我不確定如何在沒有每次遞歸調用的情況下不斷重寫它的方法。C中的遞歸目錄
更新:我已經修復了原來的錯誤,並認爲我會在這裏發佈我的新代碼。我沒有意識到的是opendir(「。」)和changedir(「..」)實際上會將週期轉換爲完整的當前或前一個路徑。至於將type = 8和type = 4語句更改爲更易讀的S_ISDIR(statbuf.st_mode)和S_ISREG(statbuf.st_mode)語句,在類型語句執行時,它們看起來根本不起作用。不知道語法和我嘗試使用它們的方式有什麼問題。
更新2:我在這裏解決了S_ISDIR/S_ISREG問題 - How to use S_ISREG() and S_ISDIR() POSIX Macros?
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
void helper(DIR *, struct dirent *, struct stat, char *, int);
void dircheck(DIR *, struct dirent *, struct stat, char *, int);
int main(int argc, char *argv[]){
DIR *dip;
struct dirent *dit;
struct stat statbuf;
char currentPath[FILENAME_MAX];
int depth = 0; /*Used to correctly space output*/
dip = opendir(".");
getcwd(currentPath, FILENAME_MAX);
while((dit = readdir(dip)) != NULL){
/*Skips . and ..*/
if(strcmp(dit->d_name, ".") == 0 || strcmp(dit->d_name, "..") == 0)
continue;
stat(currentPath, &statbuf);
/*Checks if current item is of the type file (type 8)*/
if(dit->d_type == 8)
printf("%s (%d bytes)\n", dit->d_name, (int)statbuf.st_size);
/*Checks if current item is of the type directory (type 4)*/
if(dit->d_type == 4)
dircheck(dip, dit, statbuf, currentPath, depth);
}
return 0;
}
/*Recursively called helper function*/
void helper(DIR *dip, struct dirent *dit, struct stat statbuf, char currentPath[FILENAME_MAX], int depth){
int i = 0;
dip = opendir(currentPath);
while((dit = readdir(dip)) != NULL){
if(strcmp(dit->d_name, ".") == 0 || strcmp(dit->d_name, "..") == 0)
continue;
stat(currentPath, &statbuf);
if(dit->d_type == 8){
for(i = 0; i < depth; i++)
printf(" ");
printf("%s (%d bytes)\n", dit->d_name, (int)statbuf.st_size);
}
if(dit->d_type == 4)
dircheck(dip, dit, statbuf, currentPath, depth);
}
}
void dircheck(DIR *dip, struct dirent *dit, struct stat statbuf, char currentPath[FILENAME_MAX], int depth){
int i = 0;
strcat(currentPath, "/");
strcat(currentPath, dit->d_name);
/*If two directories exist at the same levelt the path
is built wrong and needs to be corrected*/
if((chdir(currentPath)) == -1){
chdir("..");
getcwd(currentPath, FILENAME_MAX);
strcat(currentPath, "/");
strcat(currentPath, dit->d_name);
for(i = 0; i < depth; i++)
printf (" ");
printf("%s (subdirectory)\n", dit->d_name);
depth++;
helper(dip, dit, statbuf, currentPath, depth);
}
else{
for(i =0; i < depth; i++)
printf(" ");
printf("%s (subdirectory)\n", dit->d_name);
chdir(currentPath);
depth++;
helper(dip, dit, statbuf, currentPath, depth);
}
}
您好像已經回答了您自己的問題... – 2011-02-06 19:27:29
@Oli:重讀。 OP表示目前的代碼中存在一個錯誤。此外,這將是一個非常有用的學習練習,用於學習如何儘可能在遞歸的每個級別最好地重用現有緩衝區,而不是在堆上分配大量內存(或更糟的是,在堆棧上)。 – 2011-02-06 19:34:25
`type == 8`? `readdir`手冊頁中的符號常量是有原因的。 – asveikau 2011-02-06 20:55:52