使用boost::filesystem递归地列出目录文件
list directory files recursively with boost::filesystem
我使用新的boost, v1.5.3,像下面这样执行这个任务,这要归功于类recursive_directory_iterator(我不必编写递归代码):
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());
}
}
具有特定扩展名的函数列表文件。此函数适用于某些情况,但经常返回"断言失败错误",如:
**** Internal program error - .... assertion (m_imp.get()) ... operations.hpp(952): dereference of end recursive_directory_iterator
我几乎想不出这个错误的原因。有人能试试吗?赶上帮助吗?提前感谢您的帮助
您在循环内以及在for
声明中增加rdi
:
for (; rdi != end_rdi; rdi++)
{
rdi++;
这意味着rdi
可能是循环中的end_rdi
(结束迭代器,这意味着经过最后一个元素)。你这么做有什么原因吗?(如果这是故意的,您应该检查确保在增加rdi != end_rdi
之后再次检查。)
你可以尝试这样做:
recursive_directory_iterator dir(path(Dir));
for(auto&& i : dir) {
if (is_directory(i)) {
//Do whatever you want
cout << i << endl;
}
}
相关文章:
- boost::文件系统::recursive_directory_iterator多线程安全
- 使用 Boost.Spirit 解析具有混合数据类型的 OBJ 文件?
- 避免使用 boost::进程间::消息队列创建文件
- 使用Boost文件系统C++将具有特定扩展名的文件的名称保存在特定文件夹中
- Boost program_options将多个配置文件解析结果存储到一个parsed_options中
- 告诉CMake链接到自定义文件夹中的Boost
- 致命错误:找不到'boost/uuid/uuid.hpp'文件
- 使用boost::mapped_region增长文件以供进一步写入?
- 造成致命错误:boost/fusion/iterator/equal_to.hpp 没有这样的文件或目录
- 通过boost asio iostream下载大文件的最快方法是什么?
- 将 boost::文件系统::p ath 转换为 char?
- 使用 boost::regex 从目录中获取带有一些正则表达式的文件名称时出现意外输出
- 可以将Boost消息队列文件重定向到用户指定的位置
- boost::p rogram_options 配置文件格式
- 如何在Boost::program_options配置文件中为非字符串的自定义选项值类型处理空格
- boost日志文件无法创建sample.log文件
- 如何修复"无法打开包含文件'boost/config.hpp':没有这样的文件或目录(包括目录涵盖:C:\程序文件\boost\boost_1_67_0)
- C 可以在程序执行过程中移动内存映射的文件(boost :: intercecess)移动
- 包括cmake头文件boost.hpp和cmake
- 查找最后添加的文件(boost)