c++ 利用boost 实现文件操作
2018-01-30 19:44
477 查看
对文件夹里面的文件进行遍历操作是基本技能之一,python,perl以及bash等脚本都很好的实现了文件遍历方法,对于c/c++来说,只能通过系统自定的api获取。虽然文件夹操作本身是调用操作系统内核的接口,但毕竟接口不够友好。
boost不愧是准标准库,filesystem提供了极为简便的方法,如下所示:
然而,每次写程序都要附上如此一大篇代码,很不美观。故作了简单封装。
.h文件
.cpp文件
调用构造函数,传入文件夹的名字,通过mGetSubFiles()函数就可以返回文件夹内的文件路径
转载地址:http://blog.csdn.net/zyaiwx/article/details/11659335
感谢作者 zybjtu
boost不愧是准标准库,filesystem提供了极为简便的方法,如下所示:
[cpp] view plain copy // filesystem tut3.cpp ---------------------------------------------------------------// // Copyright Beman Dawes 2009 // Distributed under the Boost Software License, Version 1.0. // See http://www.boost.org/LICENSE_1_0.txt // Library home page: http://www.boost.org/libs/filesystem #include <iostream> #include <iterator> #include <algorithm> #include <boost/filesystem.hpp> using namespace std; using namespace boost::filesystem; int main(int argc, char* argv[]) { if (argc < 2) { cout << "Usage: tut3 path\n"; return 1; } path p (argv[1]); // p reads clearer than argv[1] in the following code try { if (exists(p)) // does p actually exist? { if (is_regular_file(p)) // is p a regular file? cout << p << " size is " << file_size(p) << '\n'; else if (is_directory(p)) // is p a directory? { cout << p << " is a directory containing:\n"; copy(directory_iterator(p), directory_iterator(), // directory_iterator::value_type ostream_iterator<directory_entry>(cout, "\n")); // is directory_entry, which is // converted to a path by the // path stream inserter } else cout << p << " exists, but is neither a regular file nor a directory\n"; } else cout << p << " does not exist\n"; } catch (const filesystem_error& ex) { cout << ex.what() << '\n'; } return 0; }
然而,每次写程序都要附上如此一大篇代码,很不美观。故作了简单封装。
.h文件
[cpp] view plain copy #ifndef DIRFILEOPT_HHHH #define DIRFILEOPT_HHHH #include <iostream> #include <vector> #include <string> using std::vector; using std::string; class CFileOpt { private: bool m_bIsDir; bool m_bIsFile; char* m_pFileName; bool mDirOrFile(); public: CFileOpt(char*); vector<string>& mGetSubFiles(vector<string>& lstpFileNames); ~CFileOpt(); };#endif
.cpp文件
[cpp] view plain copy #define _SCL_SECURE_NO_WARNINGS #include "FileOpt.h" #include <iterator> #include <algorithm> #include <boost/filesystem.hpp> #include <boost/algorithm/string/classification.hpp> #include <boost/algorithm/string.hpp> using namespace std; using namespace boost::filesystem; bool CFileOpt::mDirOrFile() { if(NULL == m_pFileName) return false; path p(m_pFileName); try{ if(exists(p)){ if (is_regular_file(p)) m_bIsFile = true; else if (is_directory(p)){ m_bIsDir = true; } }else{ return false; } }catch (const filesystem_error& ex){ #ifdef DEBUG printf(ex.what()); #endif } return true; } CFileOpt::CFileOpt(char* pfilename): m_pFileName(pfilename),m_bIsDir(false),m_bIsFile(false){ mDirOrFile(); } vector<string>& CFileOpt::mGetSubFiles(vector<string>& lstpFileNames) { if(m_bIsDir){ path p(m_pFileName); typedef vector<path> vec; // store paths, vec pathes; #ifdef DEBUG copy(directory_iterator(p), directory_iterator(),ostream_iterator<directory_entry>(cout,"\n")); #endif copy(directory_iterator(p), directory_iterator(), back_inserter(pathes)); for(auto iter = pathes.begin();iter != pathes.end();iter ++){ lstpFileNames.push_back(iter->generic_string()); } return lstpFileNames; }else{ #ifdef DEBUG printf("No SubFiles In %s\n",m_pFileName); #endif } return lstpFileNames; } CFileOpt::~CFileOpt(){ m_pFileName = NULL; }
调用构造函数,传入文件夹的名字,通过mGetSubFiles()函数就可以返回文件夹内的文件路径
转载地址:http://blog.csdn.net/zyaiwx/article/details/11659335
感谢作者 zybjtu
相关文章推荐
- c++ 利用boost 实现文件操作
- c++ 利用boost 实现文件操作
- 利用Boost实现高度可移植的多线程C++程序
- 利用C++标注库实现文件的读写
- PB利用API-SHFileOperation 文件操作函数 实现多文件及文件夹的删除复制等操作
- Java程序员从笨鸟到菜鸟之(一百零五)java操作office和pdf文件(三)利用jxl实现数据导出excel报表以及与POI的区别
- C++编程思想3-利用C++进行文件操作封装C函数
- c++用boost的property_tree库来操作xml配置文件
- 利用smbclient实现操作windows共享文件
- 使用C++结合文件操作和链表实现学生成绩管理系统
- Java程序员从笨鸟到菜鸟之(一百零四)java操作office和pdf文件(二)利用POI实现数据导出excel报表
- C++:利用两个队列实现栈的操作
- java Web项目中,利用其过滤功能,实现访问者每次访问服务器时,记录访问者的IP,访问时间,Url等信息,并保存到文件的操作
- 利用C/C++实现从文件读入到子程序中调用返回结果
- asp下利用fso实现文件夹或文件移动改名等操作函数
- fstream提供了三个类,用来实现c++对文件的操作。(文件的创建、读、写)。 ifstream -- 从已有的文件读 ofstream -- 向文件写内容 fstream - 打开文件供读写
- 如何利用C/C++逐行读取txt文件中的字符串(可以顺便实现文本文件的复制)
- xml学习(三)---利用xml文件实现数据岛功能(网页显示xml文件中的内容免C#代码操作xml文件)
- 文件部分-----(2)ASCII码文件读操作------------利用函数实现
- 简单文件操作(Linux下C++实现)