我正在使用下面的代码,改编自这个线程。我能够获取文件夹中的文件列表,但最后出现分段错误。知道为什么会这样吗?
有没有办法在 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
}
}
也许你会错过这些行:
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