简体   繁体   English

C++ 将目录中的所有文件与::文件系统进行比较

[英]C++ comparing all files in a directory with ::filesystem

I'm trying to compare all of the files in a directory.我正在尝试比较目录中的所有文件。

The directory is constantly getting new files added to it from a different thread.该目录不断从不同的线程中添加新文件。 Though the directory_iterator documentation says that the change may not be reflected, so I have solved this with an outer loop which tries again when the inner loop is done:虽然 directory_iterator 文档说更改可能不会反映出来,所以我用一个外部循环解决了这个问题,当内部循环完成时再次尝试:

void mergeFolder(int aFolderNum)
{
bool aPreviousMergesFinished = false;       
while (!aPreviousMergesFinished)
    {
        filesystem::directory_iterator aEnd;
        for (filesystem::directory_iterator aIterator1("data/temp/MERGEME/" + std::to_string(aFolderNum)), 
                aIterator2 = ++aIterator1;
            aIterator2 != aEnd; ++aIterator1, ++aIterator2)
        {
            std::cout << "file 1: " << aIterator1->path() << std::endl;
            std::cout << "file 2: " << aIterator2->path() << std::endl;
        }
        std::cout << "Finished inner loop" << std::endl;

        aPreviousMergesFinished =
            std::filesystem::is_empty("data/temp/MERGEME/" + std::to_string(aFolderNum));
    }
}

However, the output is as follows:但是,output如下:

... ...

file 1: "data/temp/MERGEME/1\1_999"文件 1:“data/temp/MERGEME/1\1_999”

file 2: "data/temp/MERGEME/1\1_999"文件 2:“data/temp/MERGEME/1\1_999”

file 1: "data/temp/MERGEME/1\1_9991"文件 1:“data/temp/MERGEME/1\1_9991”

file 2: "data/temp/MERGEME/1\1_9991"文件 2:“data/temp/MERGEME/1\1_9991”

file 1: "data/temp/MERGEME/1\1_9993"文件 1:“data/temp/MERGEME/1\1_9993”

file 2: "data/temp/MERGEME/1\1_9993"文件 2:“data/temp/MERGEME/1\1_9993”

file 1: "data/temp/MERGEME/1\1_9995"文件 1: "data/temp/MERGEME/1\1_9995"

file 2: "data/temp/MERGEME/1\1_9995"文件 2:“data/temp/MERGEME/1\1_9995”

file 1: "data/temp/MERGEME/1\1_9997"文件 1:“data/temp/MERGEME/1\1_9997”

file 2: "data/temp/MERGEME/1\1_9997"文件 2:“data/temp/MERGEME/1\1_9997”

file 1: "data/temp/MERGEME/1\1_9999"文件 1:“data/temp/MERGEME/1\1_9999”

file 2: "data/temp/MERGEME/1\1_9999"文件 2:“data/temp/MERGEME/1\1_9999”

Finished inner loop完成内循环

As you can see, each file is being printed twice in the inner loop, but I'm almost certain I've done the inner loop correctly.如您所见,每个文件都在内循环中打印了两次,但我几乎可以肯定我已经正确地完成了内循环。 What has gone wrong here?这里出了什么问题?

directory_iterator is only an input iterator , not a forward iterator . directory_iterator只是一个输入迭代器,而不是前向迭代器 This means that it guarantees correct behavior only on one single pass.这意味着它仅在一次通过时保证正确的行为。

As soon as you increment the directory_iterator any copies referring to the previous state are invalidated.一旦您递增directory_iterator ,任何引用先前 state 的副本都会失效。 Using them causes undefined behavior.使用它们会导致未定义的行为。

What you are trying here is not possible.你在这里尝试的是不可能的。 Even if what you are trying was allowed, you would only iterate adjacent pairs, not all pairs.即使你正在尝试的是允许的,你也只会迭代相邻的对,而不是所有的对。

Instead, read in all the file names from one iterator pass into a container and iterate over that container with two loops.相反,从一个迭代器中读入所有文件名并传递到一个容器中,然后用两个循环遍历该容器。

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

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