使用boost :: filesystem以递归方式列出目录文件

TSL*_*SL_ 2 c++ boost runtime-error boost-filesystem

由于类recursive_directory_iterator(我不必编写递归代码),我正在使用new boost,v1.5.3来执行此任务,如下所示:

void ListDirRec(const char *Dir, vector<string>& DirFileList, const char* ext)
{    
recursive_directory_iterator rdi(Dir);  
recursive_directory_iterator end_rdi;

DirFileList.empty();

string ext_str0(ext);   
for (; rdi != end_rdi; rdi++)
{
    rdi++;
    //cout << (*di).path().string() << endl;
    cout << (*rdi).path().string() << endl;

    //cout << " <----- " << (*rdi).path().extension() << endl;

    //string ext_str1 = (*rdi).path().extension().string();
    if (ext_str0.compare((*rdi).path().extension().string()) == 0)
    {
        DirFileList.push_back((*rdi).path().string());
    }
}
Run Code Online (Sandbox Code Playgroud)

具有特定扩展名的功能列表文件.此函数适用于案例,但经常返回"断言失败错误",如:

**** Internal program error - .... assertion (m_imp.get()) ... operations.hpp(952): dereference of end recursive_directory_iterator
Run Code Online (Sandbox Code Playgroud)

我几乎找不到这个错误的原因.可以尝试..抓住帮助吗?提前感谢您的帮助

Ste*_*Lin 5

您正在rdi循环内部以及for声明中递增:

for (; rdi != end_rdi; rdi++)
{
    rdi++;
Run Code Online (Sandbox Code Playgroud)

这意味着你的循环中rdi可能是end_rdi(end-iterator,意味着超过最后一个元素).你有没有理由这样做?(如果这是故意的,你应该检查以确保rdi != end_rdi在增加之后再次确认.)