简体   繁体   English

在Unix C ++中的文件夹根目录中列出文件和目录

[英]List files and directories in the root of a folder in Unix C++

I've been given an assignment which requires us to do something with all files in a given directory. 我被分配了一项工作,要求我们对给定目录中的所有文件进行处理。 For each directory encountered, we're supposed to fork off another instance of our program. 对于遇到的每个目录,我们应该派生出程序的另一个实例。 My initial approach uses opendir() and readdir() , but I've found that readdir() is enumerating all entries in the directory (not just the top level items), so some entries would be processed multiple times (once by the parent process, and once for each child process until the file is in a "root"). 我最初的方法使用opendir()readdir() ,但是我发现readdir()枚举了目录中的所有条目(而不仅仅是顶级项),因此某些条目将被多次处理(一次由父级处理)进程,并且对于每个子进程一次,直到文件位于“根”目录中。 For example, given the following file system tree: 例如,给定以下文件系统树:

.
├── 1.txt
├── a
│   ├── 2.txt
│   ├── 3.txt
│   └── c
│       └── 4.txt
└── b
    └── 5.txt

If I invoke my program on . 如果我在上调用程序. , it would process 1.txt , and create two child processes, one each for a and b and then wait for those processes to finish. ,它将处理1.txt ,并创建两个子进程,每个子进程分别用于ab ,然后等待这些进程完成。

The first child process works on 2.txt and 3.txt , and creates another child process for c . 第一个子进程适用于2.txt3.txt ,并为c创建另一个子进程。

The third child process works on 5.txt 第三个子进程在5.txt5.txt

Put Simply : I have no clue how to read only one level of a directory 简而言之 :我不知道如何只读取目录的一个级别

I could continue using my initial approach, but I feel like it would really inefficient to just ignore everything that's not in the immediate folder I'm currently inspecting. 我可以继续使用最初的方法,但是我觉得仅忽略我当前正在检查的直接文件夹中没有的所有内容实际上是无效的。


EDIT 1 : Example code: 编辑1 :示例代码:

int process(std::string binpath, std::string src)
{
        DIR* root = nullptr;

        root = opendir(source.c_str());

        if(root == nullptr)
        {
            return -1;
        }

        struct dirent* details;
        struct stat file;
        std::string path;

        std::queue<int> childPids;

        bool error = false;

        while((details = readdir(root)) != nullptr)
        {
            path = source + details->d_name;
            lstat(path.c_str(), &file);

            if(IsDirectory(file.st_mode))
            {
                if(strcmp(details->d_name, ".") == 0 || strcmp(details->d_name, "..") == 0)
                {
                    continue;
                }

                // Fork and spawn new process and remember child pid
                auto pid = fork();
                if(pid == 0)
                {
                    char* args[] = {
                            (char*) "-q",
                            (char*)"-s", const_cast<char*>(path.c_str()),
                            NULL
                    };
                    char* env[] = {NULL};

                    execve(binpath.c_str(), args, env);
                }
                else
                {
                    childPids.push(pid);
                }
            }
            else
            {
                if(!DoWorkOnFile(path)) error = true;
            }
        }

        //Wait all child pids

        while(!childPids.empty())
        {
            auto pid = childPids.front();

            int status;
            if(waitpid(pid, &status, 0) == pid)
            {
                if(status != 0) error = true;
            }
            else
            {
                error = true;
            }

            childPids.pop();
        }

        closedir(root);

        return error ? -1 : 0;
}

So let's assume you use dirent and have a code like from following tutorial: http://www.dreamincode.net/forums/topic/59943-accessing-directories-in-cc-part-i/ 因此,假设您使用dirent,并且具有类似以下教程的代码: http : //www.dreamincode.net/forums/topic/59943-accessing-directories-in-cc-part-i/

You have to distinguish between file and directory which is offered by the dirent structure through d_type. 您必须通过d_type区分dirent结构提供的文件和目录。

Take a look here: http://www.gnu.org/software/libc/manual/html_node/Directory-Entries.html 在这里看看: http : //www.gnu.org/software/libc/manual/html_node/Directory-Entries.html

That way you can make him list only the files of one directory. 这样,您可以使他仅列出一个目录的文件。

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

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