C++ 使用Boost遍历目录失败

C++ 使用Boost遍历目录失败,c++,boost,C++,Boost,下面的函数有点问题。应该给它一个路径和一组允许的文件扩展名,然后查找该路径中所有具有这些扩展名的文件。相反,它什么也找不到并返回一个空集 std::set<boostfs::path> scan_directory(const boostfs::path& p, const bool recurse, const std:

下面的函数有点问题。应该给它一个路径和一组允许的文件扩展名,然后查找该路径中所有具有这些扩展名的文件。相反,它什么也找不到并返回一个空集

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 ;
}
我使用路径“test/cpp/test.cpp”调用函数,递归为true,并且一个集合包含一个字符串“.cpp”。我从指纹中得到以下输出

path: test/cpp/test.cpp
dir: test
path: test
file: cpp
ext:
然后函数结束,程序的其余部分继续,只是给了它一组空文件,所以没有多少工作要做。给定测试目录,它应该返回一个包含“test/cpp/foo.cpp”和“test/cpp/bar/baz.cpp”的集合


我相当肯定它不久前就开始工作了,但我不确定它是什么时候坏的,也不确定是什么原因造成的。我敢肯定这是一些小的、恼人的技术问题。

我发现了我的问题。我使用的是
path::iterator
而不是
directory\u iterator
(或
recursive\u directory\u iterator
),因此我循环遍历路径的组件,而不是目录的内容。我可以早一点发誓,但那可能只是运气

这是我的工作代码

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 ;
}
std::设置扫描目录(const boostfs::path&p,
常量布尔递归,
常量标准::设置和允许){
std::字符串扩展;
std::设置INC;
//提取目录和文件名
boostfs::path file=p.filename();
boostfs::path dir=p.parent_path();
递归目录迭代器itr(dir),itr\U end;
while(itr!=itr_end){
if(boostfs::is_目录(*itr)){
itr.无推送(!递归);
}否则{
//不要包含原始来源
如果(*itr!=p){
//仅包括允许的文件类型
ext=itr->path().extension().string();
if(allowed.find(ext)!=allowed.end()){
增加插入(*itr);
}
}
}
itr++;
}
返回INC;
}

我会让大家知道,Boost文档中遍历目录的示例非常糟糕

注意,递归目录扫描的更好替代方法是
fs::recursive_directory_iterator
@Xeo谢谢,我会研究一下。现在我只想了解这里出了什么问题。为什么不使用调试器来逐步检查代码?提示:如果传递的路径没有父路径,则函数什么也不做。@JohnZwinck我从未真正使用过调试器。我想,对于这样相对简单的代码,使用调试器要比理解代码或在这里或那里插入一些打印内容更容易。很高兴您发现了问题。你知道Boost是开源的吧?你可以提交一个文档补丁@我不确定我是否有资格,但也许我能做到。
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 ;
}