我正在使用以下代码,改编自this线程。我能够获取文件夹中的文件列表,但最后我得到了分段错误。知道为什么会这样吗? 有没有办法在for循环中获取当前文件(完整路径)的std :: string?
boost::filesystem::path path("my_path");
static void myfunction()
{
boost::filesystem::directory_iterator end_itr;
// path is a static variable, defined somewhere else
for(boost::filesystem::directory_iterator dir_iter(path);
dir_iter != end_itr; ++dir_iter)
if( boost::filesystem::is_regular_file(dir_iter->status()) )
{
// print
}
}
答案 0 :(得分:0)
也许你会错过这些界限:
namespace fs = boost::filesystem;
fs::directory_iterator end_itr;
并尝试
cout << "entry: " << dir_iter->path().filename() << "\n";
cout << "entry path: " << dir_iter->path() << "\n";
循环中的
参考文献: - http://www.boost.org/doc/libs/1_49_0/libs/filesystem/v3/doc/reference.html#Class-directory_entry - http://www.boost.org/doc/libs/1_49_0/libs/filesystem/v3/doc/reference.html#class-path