对文件夹里面的文件进行遍历操作是基本技能之一,python,perl以及bash等脚本都很好的实现了文件遍历方法,对于c/c++来说,只能通过系统自定的api获取。虽然文件夹操作本身是调用操作系统内核的接口,但毕竟接口不够友好。       boost不愧是准标准库,filesystem提供了极为简便的方法,如下所示:
[cpp] view plaincopyprint?
  1. //  filesystem tut3.cpp  ---------------------------------------------------------------//
  2. //  Copyright Beman Dawes 2009
  3. //  Distributed under the Boost Software License, Version 1.0.
  4. //  See http://www.boost.org/LICENSE_1_0.txt
  5. //  Library home page: http://www.boost.org/libs/filesystem
  6. #include <iostream>
  7. #include <iterator>
  8. #include <algorithm>
  9. #include <boost/filesystem.hpp>
  10. using namespace std;
  11. using namespace boost::filesystem;
  12. int main(int argc, char* argv[])
  13. {
  14. if (argc < 2)
  15. {
  16. cout << "Usage: tut3 path\n";
  17. return 1;
  18. }
  19. path p (argv[1]);   // p reads clearer than argv[1] in the following code
  20. try
  21. {
  22. if (exists(p))    // does p actually exist?
  23. {
  24. if (is_regular_file(p))        // is p a regular file?
  25. cout << p << " size is " << file_size(p) << '\n';
  26. else if (is_directory(p))      // is p a directory?
  27. {
  28. cout << p << " is a directory containing:\n";
  29. copy(directory_iterator(p), directory_iterator(),  // directory_iterator::value_type
  30. ostream_iterator<directory_entry>(cout, "\n"));  // is directory_entry, which is
  31. // converted to a path by the
  32. // path stream inserter
  33. }
  34. else
  35. cout << p << " exists, but is neither a regular file nor a directory\n";
  36. }
  37. else
  38. cout << p << " does not exist\n";
  39. }
  40. catch (const filesystem_error& ex)
  41. {
  42. cout << ex.what() << '\n';
  43. }
  44. return 0;
  45. }
然而,每次写程序都要附上如此一大篇代码,很不美观。故作了简单封装。.h文件
[cpp] view plaincopyprint?
  1. #ifndef DIRFILEOPT_HHHH
  2. #define DIRFILEOPT_HHHH
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>
  6. using std::vector;
  7. using std::string;
  8. class CFileOpt
  9. {
  10. private:
  11. bool m_bIsDir;
  12. bool m_bIsFile;
  13. char* m_pFileName;
  14. bool mDirOrFile();
  15. public:
  16. CFileOpt(char*);
  17. vector<string>& mGetSubFiles(vector<string>& lstpFileNames);
  18. ~CFileOpt();
  19. };#endif
.cpp文件
[cpp] view plaincopyprint?
  1. #define _SCL_SECURE_NO_WARNINGS
  2. #include "FileOpt.h"
  3. #include <iterator>
  4. #include <algorithm>
  5. #include <boost/filesystem.hpp>
  6. #include <boost/algorithm/string/classification.hpp>
  7. #include <boost/algorithm/string.hpp>
  8. using namespace std;
  9. using namespace boost::filesystem;
  10. bool CFileOpt::mDirOrFile()
  11. {
  12. if(NULL == m_pFileName)
  13. return false;
  14. path p(m_pFileName);
  15. try{
  16. if(exists(p)){
  17. if (is_regular_file(p))
  18. m_bIsFile = true;
  19. else if (is_directory(p)){
  20. m_bIsDir = true;
  21. }
  22. }else{
  23. return false;
  24. }
  25. }catch (const filesystem_error& ex){
  26. #ifdef DEBUG
  27. printf(ex.what());
  28. #endif
  29. }
  30. return true;
  31. }
  32. CFileOpt::CFileOpt(char* pfilename):
  33. m_pFileName(pfilename),m_bIsDir(false),m_bIsFile(false){
  34. mDirOrFile();
  35. }
  36. vector<string>& CFileOpt::mGetSubFiles(vector<string>& lstpFileNames)
  37. {
  38. if(m_bIsDir){
  39. path p(m_pFileName);
  40. typedef vector<path> vec;             // store paths,
  41. vec pathes;
  42. #ifdef DEBUG
  43. copy(directory_iterator(p), directory_iterator(),ostream_iterator<directory_entry>(cout,"\n"));
  44. #endif
  45. copy(directory_iterator(p), directory_iterator(), back_inserter(pathes));
  46. for(auto iter = pathes.begin();iter != pathes.end();iter ++){
  47. lstpFileNames.push_back(iter->generic_string());
  48. }
  49. return lstpFileNames;
  50. }else{
  51. #ifdef DEBUG
  52. printf("No SubFiles In %s\n",m_pFileName);
  53. #endif
  54. }
  55. return lstpFileNames;
  56. }
  57. CFileOpt::~CFileOpt(){
  58. m_pFileName = NULL;
  59. }
调用构造函数,传入文件夹的名字,通过mGetSubFiles()函数就可以返回文件夹内的文件路径。

c++ 利用boost 实现文件操作相关推荐

  1. python通过内置的什么函数打开一个文件_利用python进行文件操作

    这篇文章主要介绍了如何利用python进行文件操作,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下 什么是文件 文件是系统存储区域的一个命名位置,用来存储一些信息,便于后续访问.能够在非 ...

  2. 利用HOOKAPI拦截文件操作

    先读下HookAPI 使用文档: 功能简介 HookAPI 是一个截获Windows 32位API函数的开发包,它可以在Windows调用某个API函数的时候,先调用自己编写的函数,从而实现特殊的功能 ...

  3. 趣学 C 语言(十二)—— 文件操作

    对于 C 语言而言,无论是标准输入 stdin,还是标准输出 stdout,还是标准错误输出 stderr,本质上都是一种文件操作,只不过读写的文件变成了控制台(console),或者说 stdin/ ...

  4. 【C语言】文件操作总结

    目录 前言 1. 为什么使用文件? 2. 什么是文件? 2.1 程序文件 2.2 数据文件 2.3 输入输出的类比 2.4文件名 3. 文件的打开和关闭 3.1 文件指针 3.2 文件的打开和关闭 3 ...

  5. python目录及文件操作_python路径及文件操作,10.22,10.27

    csv 和ini文件处理 作者: 一个小菜鸡 csv常见格式 nums     options      arguments 逗号分割值 comma-Separated Values CSV是一个被行 ...

  6. (转载)Mac系统下利用ADB命令连接android手机并进行文件操作

    Mac系统下利用ADB命令连接android手机并进行文件操作 标签: Mac adb android 2016-03-14 10:09 5470人阅读 评论(1) 收藏 举报  分类: Androi ...

  7. 【python文件操作之利用os筛选出想要的数据集】

    [python文件操作之利用os筛选出想要的数据集] 文章目录 [python文件操作之利用os筛选出想要的数据集] 前言 一.OS模块是什么? 二.使用步骤 1.引入库 2.读入数据 3.观察数据, ...

  8. c语言文件操作函数(未完待续)

    while(!feof(fp1)),使用feof()的时候会比你期望的多执行一次.因为它检测到结尾的'\0',时仍然不是文件的结尾,所以会继续执行一次.为解决这个问题,可以先读,在判断,然后打印 fr ...

  9. linux下怎么批量命名文件,linux下的文件操作——批量重命名

    概述:在日常工作中,我们经常需要对一批文件进行重命名操作,例如将所有的jpg文件改成bnp,将名字中的1改成one,等等.文本主要为你讲解如何实现这些操作 1.删除所有的 .bak 后缀: renam ...

最新文章

  1. 【原创】StreamInsight查询系列(六)——基本查询操作之分组聚合
  2. 神经网络如何「动」起来?| 「动态神经网络」的六大待解难题
  3. java 微信转账_实现微信转账功能
  4. 30 天精通 RxJS (01):认识 RxJS
  5. Web游戏开发编程:最神奇的“触觉振动”
  6. 是的,我更喜欢这样的工程师
  7. php foreach创建文件,php – mkdir()在foreach函数中跳过第一个文件
  8. 青蛙跳台阶(剑指 Offer 10- II)
  9. 《系统集成项目管理》第四章 项目管理一般知识
  10. 形式语言与自动机学习心得
  11. 利用IXCHARIOT进行网络测速
  12. centos 6.5 thinkpad trackpoint 中间键 滚动设置
  13. 方差分析 球形检验_方差分析的前提,与检验,以及球形检验
  14. 丈夫三次“买凶杀妻” 妻子毫无所觉称婚姻甜蜜
  15. PRIMES is in P
  16. html5怎么做成五行五列,word将文字转换成五行五列的表格怎么做
  17. 获得淘宝app商品详情原数据 API 的图解
  18. 春节快乐小目标增长计划
  19. 1.Excel查询重复数据
  20. 厦门谋划成立大数据产业基金

热门文章

  1. python中的抽象含义_Python中下划线的5种含义你都知道吗?
  2. matlab 子图title的位置_Plotly_多个子图
  3. hdp分享码2020_和平精英2020黄金风衣龙cdk兑换码
  4. 手机照片导入电脑步骤_手机录屏及电脑录屏操作步骤
  5. 华硕服务器 u盘安装系统,华硕用u盘如何安装系统
  6. 【c++】29.设计模式总结
  7. Java NIO学习系列六:Java中的IO模型
  8. 程序员面试题精选100题(27)-二元树的深度[数据结构]
  9. STL的红与黑--rb_tree红黑树
  10. 数字图像处理:第二章 图象获取、显示、表示与处理