我有这段 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/