c++ 利用boost 实现文件操作

 对文件夹里面的文件进行遍历操作是基本技能之一,python,perl以及bash等脚本都很好的实现了文件遍历方法,对于c/c++来说,只能通过系统自定的api获取。虽然文件夹操作本身是调用操作系统内核的接口,但毕竟接口不够友好。

       boost不愧是准标准库,filesystem提供了极为简便的方法,如下所示:

  1. //  filesystem tut3.cpp  ---------------------------------------------------------------//  
  2.   
  3. //  Copyright Beman Dawes 2009  
  4.   
  5. //  Distributed under the Boost Software License, Version 1.0.  
  6. //  See http://www.boost.org/LICENSE_1_0.txt  
  7.   
  8. //  Library home page: http://www.boost.org/libs/filesystem  
  9.   
  10. #include <iostream>  
  11. #include <iterator>  
  12. #include <algorithm>  
  13. #include <boost/filesystem.hpp>  
  14. using namespace std;  
  15. using namespace boost::filesystem;  
  16.   
  17. int main(int argc, char* argv[])  
  18. {  
  19.   if (argc < 2)  
  20.   {  
  21.     cout << "Usage: tut3 path\n";  
  22.     return 1;  
  23.   }  
  24.   
  25.   path p (argv[1]);   // p reads clearer than argv[1] in the following code  
  26.   
  27.   try  
  28.   {  
  29.     if (exists(p))    // does p actually exist?  
  30.     {  
  31.       if (is_regular_file(p))        // is p a regular file?  
  32.         cout << p << " size is " << file_size(p) << '\n';  
  33.   
  34.       else if (is_directory(p))      // is p a directory?  
  35.       {  
  36.         cout << p << " is a directory containing:\n";  
  37.   
  38.         copy(directory_iterator(p), directory_iterator(),  // directory_iterator::value_type  
  39.           ostream_iterator<directory_entry>(cout, "\n"));  // is directory_entry, which is  
  40.                                                            // converted to a path by the  
  41.                                                            // path stream inserter  
  42.       }  
  43.       else  
  44.         cout << p << " exists, but is neither a regular file nor a directory\n";  
  45.     }  
  46.     else  
  47.       cout << p << " does not exist\n";  
  48.   }  
  49.   
  50.   catch (const filesystem_error& ex)  
  51.   {  
  52.     cout << ex.what() << '\n';  
  53.   }  
  54.   
  55.   return 0;  
  56. }  
然而,每次写程序都要附上如此一大篇代码,很不美观。故作了简单封装。

.h文件

  1. #ifndef DIRFILEOPT_HHHH  
  2. #define DIRFILEOPT_HHHH  
  3.   
  4. #include <iostream>  
  5. #include <vector>  
  6. #include <string>  
  7.   
  8. using std::vector;  
  9. using std::string;  
  10.   
  11. class CFileOpt  
  12. {  
  13. private:  
  14.     bool m_bIsDir;  
  15.     bool m_bIsFile;  
  16.     char* m_pFileName;  
  17.     bool mDirOrFile();  
  18.   
  19. public:  
  20.     CFileOpt(char*);  
  21.     vector<string>& mGetSubFiles(vector<string>& lstpFileNames);  
  22.     ~CFileOpt();  
  23. };#endif  
.cpp文件
  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.   
  11. bool CFileOpt::mDirOrFile()  
  12. {  
  13.     if(NULL == m_pFileName)  
  14.         return false;  
  15.     path p(m_pFileName);  
  16.     try{  
  17.         if(exists(p)){     
  18.             if (is_regular_file(p))         
  19.                 m_bIsFile = true;             
  20.             else if (is_directory(p)){  
  21.                 m_bIsDir = true;  
  22.             }  
  23.         }else{  
  24.             return false;  
  25.         }  
  26.     }catch (const filesystem_error& ex){  
  27. #ifdef DEBUG  
  28.         printf(ex.what());  
  29. #endif  
  30.     }  
  31.     return true;  
  32. }  
  33. CFileOpt::CFileOpt(char* pfilename):  
  34.     m_pFileName(pfilename),m_bIsDir(false),m_bIsFile(false){  
  35.         mDirOrFile();  
  36. }  
  37. vector<string>& CFileOpt::mGetSubFiles(vector<string>& lstpFileNames)  
  38. {  
  39.     if(m_bIsDir){  
  40.         path p(m_pFileName);  
  41.         typedef vector<path> vec;             // store paths,  
  42.         vec pathes;  
  43. #ifdef DEBUG  
  44.     copy(directory_iterator(p), directory_iterator(),ostream_iterator<directory_entry>(cout,"\n"));  
  45. #endif  
  46.         copy(directory_iterator(p), directory_iterator(), back_inserter(pathes));  
  47.         for(auto iter = pathes.begin();iter != pathes.end();iter ++){  
  48.             lstpFileNames.push_back(iter->generic_string());  
  49.         }  
  50.         return lstpFileNames;  
  51.     }else{  
  52. #ifdef DEBUG  
  53.         printf("No SubFiles In %s\n",m_pFileName);  
  54. #endif  
  55.     }  
  56.     return lstpFileNames;  
  57. }  
  58. CFileOpt::~CFileOpt(){  
  59.     m_pFileName = NULL;  
  60. }  
调用构造函数,传入文件夹的名字,通过mGetSubFiles()函数就可以返回文件夹内的文件路径。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值