如何递归遍历目录并在C中打印所有文件? [英] How to recursively walk through a directory and print all files in C?

查看:170
本文介绍了如何递归遍历目录并在C中打印所有文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图递归地遍历目录并打印出所有文件。

I am trying to recursively walk through a directory and print out all of the files.

当我尝试使用以下代码进行操作时,例如,我使用sprint(。,。)调用了该函数,结果得到了一堆点。

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.

当我减少允许的打开进程的数量(ulimit -n 20)时,我得到17个点。不过我不明白,因为我认为readdir()和opendir()不会产生新的进程。这是我的代码:

When I reduce the amount of open processes allowed (ulimit -n 20) I get 17 dots. I don't understand though because I thought readdir() and opendir() doesn't make new processes. 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);
    }
 }

其余文件,因为它仅打印点,而不打印完整文件名。我认为它在我的最后一个if循环中,但是我不确定。

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.

推荐答案

下面是执行该操作的递归代码:

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);
}

sprint(。, 。递归地遍历目录并打印出所有文件。

Call it with sprint(".", "."); to recursively walk through a directory and print out all of the files.

答案。

这篇关于如何递归遍历目录并在C中打印所有文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆