使用boost::filesystem实现目录遍历
下面的代码实现了深度优先和广度优先两种遍历方式,可以指定最大遍历深度,可以指定结果中是否包含子文件夹
======================================================================
#include <string>
#include <vector>
#include <deque>
#include <utility>
#include<boost/filesystem/operations.hpp>
#include<boost/filesystem/path.hpp>
class file_tool
{
public:
{
DEPTH_FIRST = 1,
BREADTH_FIRST =2,
};
enum { UNLIMITED_DEPTH =-1};
static bool get_sub_files(conststd::string& path,std::vector<std::string>&files, int max_depth = UNLIMITED_DEPTH, bool include_sub_dirs =false, traverse_order_t order = BREADTH_FIRST)
{
using namespace std;
namespace fs =boost::filesystem;
typedefstd::pair<string,int> path_and_depth_t;
deque<path_and_depth_t> qu;
{
fs::path root(path);
if(!fs::exists(root) ||!fs::is_directory(root))
{
return false;
}
if(max_depth <= 0 &&max_depth != UNLIMITED_DEPTH)
{
return true;
}
fs::directory_iteratorend_iter;
for(fs::directory_iteratorfile_itr(root); file_itr != end_iter; ++file_itr)
{
qu.push_back(path_and_depth_t(fs::system_complete(*file_itr).native_directory_string(),1));
}
}
while (!qu.empty())
{
path_and_depth_t path_and_depth = (order == DEPTH_FIRST) ?qu.back() : qu.front();
string& file_str(path_and_depth.first);
int depth= path_and_depth.second;
if (order== DEPTH_FIRST)
{
qu.pop_back();
}
else
{
qu.pop_front();
}
fs::path file(file_str);
if(fs::exists(file))
{
if(fs::is_directory(file))
{
if (include_sub_dirs)
{
files.push_back(file_str);
}
if (depth <max_depth || max_depth == UNLIMITED_DEPTH)
{
intnext_depth = depth + 1;
fs::directory_iteratorend_iter;
for(fs::directory_iteratorfile_itr(file); file_itr != end_iter; ++file_itr)
{
qu.push_back(path_and_depth_t(fs::system_complete(*file_itr).native_directory_string(),next_depth));
}
}
}
else
{
files.push_back(file_str);
}
}
}
return true;
}
};
使用boost::filesystem实现目录遍历相关推荐
- C++目录遍历:使用第三方库boost.filesystem等
1. opencv 目录文件遍历 注释:2014 0814 这个代码是基于java的,Java使用托管代码进行目录管理,C++就不那么跨平台了. 原文链接:http://blog.csdn.net/z ...
- boost::filesystem目录相关的测试程序
boost::filesystem目录相关的测试程序 实现功能 C++实现代码 实现功能 boost::filesystem目录相关的测试程序 C++实现代码 #include <boost/f ...
- Boost filesystem学习笔记
1. 头文件 #include <boost/filesystem.hpp> 2. 函数介绍 2.1. 路径判断 boost::filesystem::system_complete(pa ...
- 了解 Boost Filesystem Library文件系统
C++ 语言(实际上是 C++ 标准)的最常见问题之一是,缺乏定义良好的库来帮助处理文件系统查询和操作.由于这个原因,程序员不得不使用本机操作系统提供的应用程序编程接口(Application Pro ...
- Boost::filesystem的使用笔记
Boost::filesystem 使用小笔记 http://blog.csdn.net/dourgulf/article/details/8589207 boost 官方文档:http://en.h ...
- 了解 Boost Filesystem Library
C++ 语言(实际上是 C++ 标准)的最常见问题之一是,缺乏定义良好的库来帮助处理文件系统查询和操作.由于这个原因,程序员不得不使用本机操作系统提供的应用程序编程接口(Application Pro ...
- boost::filesystem使用入门体验
boost::filesystem使用入门体验 boost::filesystem使用入门体验 CMake的添加 头文件和命名空间 常用类和函数 boost::filesystem使用入门体验 最近开 ...
- boost::filesystem经常使用使用方法具体解释
提示: filesystem库提供了两个头文件,一个是<boost/filesystem.hpp>,这个头文件包括基本的库内容.它提供了对文件系统的重要操作. 同一时候它定义了一个类pat ...
- boost::filesystem 库的简单使用
#include<boost/filesystem.hpp> using namespace filesystem; path类 system_complete();返回绝 ...
- boost::filesystem::directory_iterator() 出现段错误 Sementation fault
这是一个复盘帖,将遇到的boost的相关问题复盘一下, 先贴上我的代码部分: if (boost::filesystem::is_directory(mypath)) {boost::filesyst ...
最新文章
- dev treeview控件_在Winform开发框架中使用DevExpress的TreeList和TreeListLookupEdit控件
- 给出a的定义 -- 指针 和 数组
- 设计模式研究(二)-Singleton
- ELK日志系统的写入优化
- Java Calendar使用指南
- 新闻发布系统类图_如何熟悉一个系统?(内含知识大图)
- oauth2.0 php简化模式,OAuth2.0学习(1-5)授权方式2-简化模式(implicit grant type)
- Python 中 PyQt5 + pycharm 调用 Qt Designer,将.ui文件转换成 .py 文件
- java创建文件夹和文件_Java创建文件夹与创建文件
- 【MySql】100问
- Pr 入门教程,如何确保剪辑保持同步?
- CSS布局:图片在DIV中上下左右居中(水平和垂直都居中)
- 给自己一个anchor,挑战不可能
- 航空三字代码表_国际国内城市三字代码查询-城市航空代码表
- 百度首页制作 (仿百度首页)
- 鼎捷软件ERP,MES等系统构筑制造企业信息化系统
- 《稻盛和夫经营学》读后感心得体会3180字范文
- win10配置ffmpeg环境
- Error 0x80240017: Failed to execute MSU package.
- 【黄啊码】thinkphp sql wherein的排序问题|tp不能按whereIn排序?
热门文章
- 挨踢部落故事汇(2):机缘所致转型之路
- redis缓存Hash操作的在主数据中的应用
- PHP 遍历文件夹及文件类及处理类
- linux文件传输阻塞了,Linux 中的read系统调用到底是阻塞还是非阻
- 解决使用七牛CDN导致AJAX评论报错{“ERROR”:”GET FROM IMAGE SOURCE FAILED: E405″}
- mysql 分表例子_mysql分表查询的简单例子
- jmeter录制脚本_jmeter(二)Badboy录制脚本
- cls_template.php on line 1067,ecshop php5.5兼容utf-8版本
- backlog配置_Nginx backlog配置概述
- 自动驾驶7-2 最终项目概述 Final Project Overview