C++遍历文件夹,使用boost filesystem递归遍历文件夹
来源:互联网 发布:网络大电影受众群体 编辑:程序博客网 时间:2024/05/22 00:33
#ifndef SCANALLFILES_H#define SCANALLFILES_H#include "boost/filesystem/operations.hpp"#include "boost/filesystem/path.hpp"#include <iostream>using namespace std;class ScanAllFiles{public: static const vector<string>& scanFiles(const string&,vector<string>&); //方法一,自己写递归,用filesystem里的directory_iterator static const vector<string>& scanFilesUseRecursive(const string&,vector<string>&); //方法二,直接用boost的filesystem里的recursive_directory_iterator};//方法一,自己写递归const vector<string>& ScanAllFiles::scanFiles(const string& rootPath,vector<string>& container=*(new vector<string>())){ namespace fs = boost::filesystem; fs::path fullpath (rootPath, fs::native); vector<string> &ret = container; if(!fs::exists(fullpath)){return ret;} fs::directory_iterator end_iter; /**无参构造函数是最后那个iterator的value 摘抄如下*If the end of the directory elements is reached, the iterator becomes equal to the end iterator value. The constructor directory_iterator() with no arguments always constructs an end iterator object, which is the only legitimate iterator to be used for the end condition. The result of operator* on an end iterator is not defined. For any other iterator value a const directory_entry& is returned. The result ofoperator-> on an end iterator is not defined. For any other iterator value a const directory_entry* is returned.***/ for(fs::directory_iterator iter(fullpath);iter!=end_iter;iter++){ try{ if (fs::is_directory( *iter ) ){ std::cout<<*iter << "is dir.whose parent path is " << iter->path().branch_path() << std::endl; ret.push_back(iter->path().string()); //递归前push_back进去一个 ScanAllFiles::scanFiles(iter->path().string(),ret);//递归,把vector也传进去 }else{ ret.push_back(iter->path().string()); std::cout << *iter << " is a file" << std::endl; } } catch ( const std::exception & ex ){ std::cerr << ex.what() << std::endl; continue; } } return ret;}//方法二,直接用boost的filesystem里的recursive_directory_iteratorconst vector<string>& ScanAllFiles::scanFilesUseRecursive(const string& rootPath,vector<string>& container=*(new vector<string>())){ namespace fs = boost::filesystem; fs::path fullpath (rootPath, fs::native); vector<string> &ret = container; if(!fs::exists(fullpath)){return ret;} fs::recursive_directory_iterator end_iter; for(fs::recursive_directory_iterator iter(fullpath);iter!=end_iter;iter++){ try{ if (fs::is_directory( *iter ) ){ std::cout<<*iter << "is dir" << std::endl; ret.push_back(iter->path().string()); //ScanAllFiles::scanFiles(iter->path().string(),ret); }else{ ret.push_back(iter->path().string()); std::cout << *iter << " is a file" << std::endl; } } catch ( const std::exception & ex ){ std::cerr << ex.what() << std::endl; continue; } } return ret;}#endif
0 0
- 使用boost filesystem递归遍历文件夹
- C++遍历文件夹,使用boost filesystem递归遍历文件夹
- C++遍历文件夹,使用boost filesystem递归遍历文件夹
- C++遍历文件夹,使用boost filesystem递归遍历文件夹
- boost递归遍历文件夹
- boost递归遍历文件夹
- 使用递归遍历文件夹
- Java使用递归遍历文件夹
- boost 遍历文件夹
- 递归法遍历文件夹
- VBS递归遍历文件夹
- 非递归遍历文件夹
- 非递归遍历文件夹
- 递归遍历文件(文件夹)
- java 递归遍历文件夹
- 非递归遍历文件夹
- 文件夹递归遍历
- php递归遍历文件夹
- 缓存是新的内存
- 负载均衡
- 如何在mac本上安装android sdk
- 《这样读书就够了》读书笔记分享
- android新技术学习(1)-2016年,这些Android技术会很火
- C++遍历文件夹,使用boost filesystem递归遍历文件夹
- android中信息列表的下拉刷新和下拉加载
- StarWind模拟iSCSI设备 [类似openfiler、FreeNAS]
- LUAMD5加密
- 解决IDEA编译通过能运行但是出现红色下划线的问题idea cant resolve symbol"xxx"
- GIS+=地理信息+云计算技术——私有云架构设计(1)计算资源规划
- 位段详解。
- poj-2155-Matrix(树状数组 || 线段树)
- Google C++编程规范 – 第二十一条 -《-inl.h文件》