C++遍历文件夹,使用boost filesystem递归遍历文件夹
2012-02-21 11:53
567 查看
编译环境vc 9
另外,转载一篇ibm的boost filesystem入门文章:http://www.ibm.com/developerworks/cn/aix/library/au-boostfs/
还有一个boost的filesystem的conference: http://www.boost.org/doc/libs/1_48_0/libs/filesystem/v3/doc/reference.html#Class-directory_entry
#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_iterator const 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
另外,转载一篇ibm的boost filesystem入门文章:http://www.ibm.com/developerworks/cn/aix/library/au-boostfs/
还有一个boost的filesystem的conference: http://www.boost.org/doc/libs/1_48_0/libs/filesystem/v3/doc/reference.html#Class-directory_entry
相关文章推荐
- C++遍历文件夹,使用boost filesystem递归遍历文件夹
- 基于c++使用win32 api遍历文件夹
- 使用C++遍历文件夹/目录
- 使用C++遍历文件夹/目录
- C++ 使用win32API遍历文件夹
- c++遍历查找文件夹路径下的所有文件(比如.txt) 使用的是MFC的CFileFind类
- 遍历文件夹下某种类型文件名(C++)
- Win10下C++代码文件夹遍历错误:0xC0000005: 写入位置时发生访问冲突。
- 【C++】遍历文件夹下的图片文件,并返回其路径
- Python与C++ 遍历文件夹下的所有图片实现代码
- C++ 压缩文件及文件夹 使用zlib开源库
- c++ 遍历文件夹
- python程序设计基础5:python文件使用(遍历一个文件夹中所有的图片)
- C++遍历获取文件夹下面所有文件名
- C++遍历文件夹
- Linux 环境下使用C++ 提取指定文件夹及其子文件夹中的文件名信息
- java.io.File类基本使用——遍历某路径的所有文件夹及文件
- C/C++遍历文件夹
- C++遍历文件夹下的所有文件