c++ - Boost.Filesystem 崩溃

标签 c++ boost boost-filesystem

有人遇到过这个问题吗?当使用 recursive_directory_iterator 搜索分区时,当它到达末尾时会崩溃。
我在带有 boost 1.39 的 Visual Studio 2008 中得到了这个,但也在家里使用带有 boost 1.46 的 MinGW 得到了这个。我不认为我做错了什么:

#include "stdafx.h"
#include <iostream>
#include <boost/filesystem.hpp>
using namespace std;




int _tmain(int argc, _TCHAR* argv[])
{
    boost::filesystem::path musicPaths("d:\\");
    for(boost::filesystem::recursive_directory_iterator it(musicPaths); it != boost::filesystem::recursive_directory_iterator(); ++it)
    {
        string strToFind = ".mp3";
        boost::filesystem::path dummypath = it->path().filename();
        string str = dummypath.string();
        if(str.find(strToFind) != -1)
        {
            cout << str << endl;
        }
    }
    return 0;
}

编辑:
我看到它最后并没有崩溃,而是当它到达系统卷信息时崩溃

最佳答案

Windows 不允许您查看“系统卷信息”目录的内部。 因此,在“系统卷信息”上释放 recursive_directory_iterator 是一个坏主意。

编辑:您也许可以使用 recursive_directory_iterator::no_push() 解决问题。来自 Boost 文档:

void no_push(bool value=true);

    Requires: *this != recursive_directory_iterator().

    Postcondition: no_push_pending() == value.

    Throws: Nothing.

    [Note: no_push() is used to prevent unwanted recursion into a directory. --end note]

关于c++ - Boost.Filesystem 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6289573/

相关文章:

c++ - 如何简化 make_variant_over 生成的类型

c++ - 检测模板化类型本身是否是模板化类型

c++ - 哪些编译器支持 std::filesystem?

c++ - Const std::filesystem::path 引用常量不受尊重,这是我做错的吗?

c++ - Cmake 中的 Boost 文件系统版本错误

c++ - 如何在 Xcode 中链接动态库?

c++ - 向前声明类模板显式/部分特化有什么意义?

c++ - 《C++ Concurrency In Action》中的可中断线程示例

c++ - 如果输入迭代器不是随机访问,如何告诉Advance()在输入迭代器上使用+=运算符

c++ - 将线性代数库与 Boost::Units 相结合