简体   繁体   English

如何递归遍历目录并打印C中的所有文件?

[英]How to recursively walk through a directory and print all files in C?

I am trying to recursively walk through a directory and print out all of the files. 我试图以递归方式遍历目录并打印出所有文件。

When I try doing it with the code below, for example I call the function with sprint(".", ".") I get an output of a bunch of dots. 当我尝试使用下面的代码时,例如我用sprint调用函数(“。”,“。”)我得到了一堆点的输出。

When I reduce the amount of open processes allowed (ulimit -n 20) I get 17 dots. 当我减少允许的开放进程数量(ulimit -n 20)时,我得到17个点。 I don't understand though because I thought readdir() and opendir() doesn't make new processes. 我不明白,因为我认为readdir()和opendir()不会创建新进程。 Here is my code: 这是我的代码:

void sprint(char *filename, char * dirToOpen) {
    DIR *directory = opendir(dirToOpen);
    struct dirent *entry;
    struct stat s;
    char pathname[1024];
    if (directory) { /*if it's a directory*/
       while ((entry = readdir(directory)) != NULL) { /*while there are more entries to read*/
          if(strcmp(entry->d_name, filename) == 0) /*if entry name corresponds to filename, print it*/
             printf("%s\n", filename);
          sprintf(pathname, "./%s", entry->d_name); /*makes pathname*/
          if (lstat(pathname, &s) == 0 && S_ISDIR(s.st_mode)) { /*if the file is a directory*/
             if(strcmp(entry->d_name, "..") != 0 && strcmp(entry->d_name, ".") != 0) /*if the directory isn't . or ..*/
                sprint(filename, entry->d_name);
          }
       }
       closedir(directory);
    }
 }

Also somewhere along the way it doesn't reach the rest of the files because it only prints dots, not the full file name. 在某些地方它也没有到达其余的文件,因为它只打印点,而不是完整的文件名。 I think it is somewhere in my last if loop but I'm not sure. 我认为这是我最后一个if循环中的某个地方,但我不确定。

Here is a recursive code that does that: 这是一个递归代码,它执行以下操作:

void sprint(char *filename, char * dirToOpen, int level = 0)
{
    DIR *dir;
    struct dirent *entry;
    struct stat s;

    if (!(dir = opendir(dirToOpen)))
        return;
    if (!(entry = readdir(dir)))
        return;

    do
    {
        if(lstat(dirToOpen, &s) == 0 && S_ISDIR(s.st_mode)) /*if it's a directory*/
        {
            char path[1024];
            int len = snprintf(path, sizeof(path)-1, "%s/%s", dirToOpen, entry->d_name); /*makes pathname*/
            path[len] = 0;
            if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) /*if the directory isn't . or ..*/
                continue;
            printf("%*s[%s]\n", level * 2, "", entry->d_name);
            sprint(filename ,path, level + 1);
        }
        else
        {
            if(strcmp(entry->d_name, filename) == 0 || strcmp(filename, ".") == 0) /*if entry name corresponds to filename, print it*/
             printf("%*s- %s\n", 2, "", entry->d_name);
        }
    } while (entry = readdir(dir)); /*while there are more entries to read*/
    closedir(dir);
}

Call it with sprint(".", "."); sprint(".", ".");调用它sprint(".", "."); to recursively walk through a directory and print out all of the files. 以递归方式遍历目录并打印出所有文件。

Inspired from this answer. 灵感来自这个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM