c++ - boost 目录迭代器

标签 c++ boost

我有这段 Boost 代码可以遍历目录中的文件:

void someFuncToIterateFiles() {

    if( exists( directory ) )
    {
       directory_iterator end ;

       for( directory_iterator iter(directory) ; iter != end ; ++iter ) {
         if ( !is_directory( *iter ) )
         {
             // this is a file 
             cout << iter->path; 

             // rest of the code
         }
       }
    }
}

我发现有时当它读取一个存在的目录并开始遍历文件时,程序只是停止并且没有错误或段错误。其余的代码位根本没有执行,函数返回给调用者这很奇怪,因为我以前没有遇到过这样的事情。不会抛出异常,除非这需要我们显式使用 try-catch 异常处理程序。

有人在使用 Boost 时遇到过这种情况吗?我不能调试,只能使用 cout 语句,因为我的一些依赖库是在 Release模式下编译的。

最佳答案

path 不是一个字段,它是一个方法,所以你应该这样调用它:

cout << iter->path();

关于c++ - boost 目录迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10509836/

相关文章:

c++ - Valgrind 无效的读/写列表

c++ - 函数内的静态 constexpr 变量是否有意义?

c++ - 优化稀疏矩阵中的对数熵计算

c++ - 在 boost::bind 中通过引用传递 vector

c++ - 使用 boost::assign::list_of

c++ - 使用相同的 fstream 对象在 C++ 中读取多个文件

C++ 多项式加法

c++ - 如何使用已经存在的 tcp::socket 创建一个 tcp::iostream?

c++ - 如何用不同类型的迭代器填充 vector ?

c++ - 使用模板类 boost python