遍历目录与升压失败 [英] Traversing directory with Boost fails
问题描述
我有一些麻烦,下面的函数。它应该被赋予一个路径和一组允许的文件扩展名,然后找到与任何这些扩展的这条道路的所有文件。相反,它觉得没有什么,并返回一个空集。
I'm having some trouble with the following function. It is supposed to be given a path and a set of allowed file extensions, then find all files in that path with any of those extensions. Instead it finds nothing and returns an empty set.
std::set<boostfs::path> scan_directory(const boostfs::path& p,
const bool recurse,
const std::set<std::string>& allowed) {
std::string ext ;
std::set<boostfs::path> incs, incs2 ;
boostfs::path::iterator itr ;
// Extract directory and filename
boostfs::path file = p.filename() ;
boostfs::path dir = p.parent_path() ;
std::cout << "path: " << p.string() << std::endl ;
for (itr = dir.begin(); itr != dir.end(); ++itr) {
if (boostfs::is_directory(*itr)) {
if (recurse) {
std::cout << "dir: " << itr->string() << std::endl ;
incs2 = scan_directory(*itr, true, allowed) ;
incs.insert(incs2.begin(), incs2.end()) ;
}
} else {
// Don't include the original source
if (*itr != p) {
// Include only allowed file types
ext = itr->extension().string() ;
std::cout << "file: " << itr->string() << std::endl ;
std::cout << "ext: " << ext << std::endl ;
if (allowed.find(ext) != allowed.end()) {
incs.insert(*itr) ;
}
}
}
}
return incs ;
}
版画来清点只是进行调试。我用下面的目录结构对其进行测试:
The prints to cout are just for debugging. I'm testing it with the following directory structure:
./test/cpp/
foo.cpp
foo.h
test.cpp
./test/cpp/bar/
baz.cpp
baz.h
我调用与路径测试/ CPP / TEST.CPP,递归真实,包含一个字符串了一套的.cpp的功能。我从打印下面的输出,
I invoke the function with the path "test/cpp/test.cpp", recurse true and a set containing one string ".cpp". I get the following output from the prints,
path: test/cpp/test.cpp
dir: test
path: test
file: cpp
ext:
然后函数结束,程序的其余部分仍在继续,只是它给了一个空集文件,所以没有太多的去努力的。鉴于test目录应该返回一个包含测试/ CPP / Foo.cpp中和test / CPP /酒吧/ baz.cpp。
Then the function ends and the rest of the program continues, only it's given an empty set of files so not much to work on. Given the test directory it should return a set containing "test/cpp/foo.cpp" and "test/cpp/bar/baz.cpp".
我相当肯定它的工作时间不长,但我不知道,当它打破了还是我做了什么,使得它这样做。我敢肯定,这是一些小的,恼人的技术性。
I'm fairly sure it worked not long ago, but I'm not sure when it broke or what I did that made it do so. I'm sure it's some small, annoying technicality.
推荐答案
我发现我的问题。我使用路径:迭代
而不是 directory_iterator
(或 recursive_directory_iterator
),所以我是通过路径,而不是该目录的内容的部件循环。我敢发誓,那前面的工作,但是这可能只是一直运气也许吧。
I found my problem. I was using path::iterator
instead of directory_iterator
(or recursive_directory_iterator
) so I was looping through the components of the path instead of the contents of the directory. I could have sworn it worked earlier, but that might just have been luck maybe.
这是我的工作code
Here's my working code
std::set<boostfs::path> scan_directory(const boostfs::path& p,
const bool recurse,
const std::set<std::string>& allowed) {
std::string ext ;
std::set<boostfs::path> incs ;
// Extract directory and filename
boostfs::path file = p.filename() ;
boostfs::path dir = p.parent_path() ;
boostfs::recursive_directory_iterator itr(dir), itr_end ;
while(itr != itr_end) {
if (boostfs::is_directory(*itr)) {
itr.no_push(!recurse) ;
} else {
// Don't include the original source
if (*itr != p) {
// Include only allowed file types
ext = itr->path().extension().string() ;
if (allowed.find(ext) != allowed.end()) {
incs.insert(*itr) ;
}
}
}
itr++ ;
}
return incs ;
}
我要让人们知道,通过Boost的文件中的目录遍历的例子太可怕了。
I'll let it be known that the examples of iterating through directories in Boost's documentation are AWFUL
这篇关于遍历目录与升压失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!