c++ 利用boost 实现文件操作
对文件夹里面的文件进行遍历操作是基本技能之一,python,perl以及bash等脚本都很好的实现了文件遍历方法,对于c/c++来说,只能通过系统自定的api获取。虽然文件夹操作本身是调用操作系统内核的接口,但毕竟接口不够友好。 boost不愧是准标准库,filesystem提供了极为简便的方法,如下所示:
- // 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文件
- #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文件
- #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()函数就可以返回文件夹内的文件路径。
c++ 利用boost 实现文件操作相关推荐
- python通过内置的什么函数打开一个文件_利用python进行文件操作
这篇文章主要介绍了如何利用python进行文件操作,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下 什么是文件 文件是系统存储区域的一个命名位置,用来存储一些信息,便于后续访问.能够在非 ...
- 利用HOOKAPI拦截文件操作
先读下HookAPI 使用文档: 功能简介 HookAPI 是一个截获Windows 32位API函数的开发包,它可以在Windows调用某个API函数的时候,先调用自己编写的函数,从而实现特殊的功能 ...
- 趣学 C 语言(十二)—— 文件操作
对于 C 语言而言,无论是标准输入 stdin,还是标准输出 stdout,还是标准错误输出 stderr,本质上都是一种文件操作,只不过读写的文件变成了控制台(console),或者说 stdin/ ...
- 【C语言】文件操作总结
目录 前言 1. 为什么使用文件? 2. 什么是文件? 2.1 程序文件 2.2 数据文件 2.3 输入输出的类比 2.4文件名 3. 文件的打开和关闭 3.1 文件指针 3.2 文件的打开和关闭 3 ...
- python目录及文件操作_python路径及文件操作,10.22,10.27
csv 和ini文件处理 作者: 一个小菜鸡 csv常见格式 nums options arguments 逗号分割值 comma-Separated Values CSV是一个被行 ...
- (转载)Mac系统下利用ADB命令连接android手机并进行文件操作
Mac系统下利用ADB命令连接android手机并进行文件操作 标签: Mac adb android 2016-03-14 10:09 5470人阅读 评论(1) 收藏 举报 分类: Androi ...
- 【python文件操作之利用os筛选出想要的数据集】
[python文件操作之利用os筛选出想要的数据集] 文章目录 [python文件操作之利用os筛选出想要的数据集] 前言 一.OS模块是什么? 二.使用步骤 1.引入库 2.读入数据 3.观察数据, ...
- c语言文件操作函数(未完待续)
while(!feof(fp1)),使用feof()的时候会比你期望的多执行一次.因为它检测到结尾的'\0',时仍然不是文件的结尾,所以会继续执行一次.为解决这个问题,可以先读,在判断,然后打印 fr ...
- linux下怎么批量命名文件,linux下的文件操作——批量重命名
概述:在日常工作中,我们经常需要对一批文件进行重命名操作,例如将所有的jpg文件改成bnp,将名字中的1改成one,等等.文本主要为你讲解如何实现这些操作 1.删除所有的 .bak 后缀: renam ...
最新文章
- 【原创】StreamInsight查询系列(六)——基本查询操作之分组聚合
- 神经网络如何「动」起来?| 「动态神经网络」的六大待解难题
- java 微信转账_实现微信转账功能
- 30 天精通 RxJS (01):认识 RxJS
- Web游戏开发编程:最神奇的“触觉振动”
- 是的,我更喜欢这样的工程师
- php foreach创建文件,php – mkdir()在foreach函数中跳过第一个文件
- 青蛙跳台阶(剑指 Offer 10- II)
- 《系统集成项目管理》第四章 项目管理一般知识
- 形式语言与自动机学习心得
- 利用IXCHARIOT进行网络测速
- centos 6.5 thinkpad trackpoint 中间键 滚动设置
- 方差分析 球形检验_方差分析的前提,与检验,以及球形检验
- 丈夫三次“买凶杀妻” 妻子毫无所觉称婚姻甜蜜
- PRIMES is in P
- html5怎么做成五行五列,word将文字转换成五行五列的表格怎么做
- 获得淘宝app商品详情原数据 API 的图解
- 春节快乐小目标增长计划
- 1.Excel查询重复数据
- 厦门谋划成立大数据产业基金
热门文章
- python中的抽象含义_Python中下划线的5种含义你都知道吗?
- matlab 子图title的位置_Plotly_多个子图
- hdp分享码2020_和平精英2020黄金风衣龙cdk兑换码
- 手机照片导入电脑步骤_手机录屏及电脑录屏操作步骤
- 华硕服务器 u盘安装系统,华硕用u盘如何安装系统
- 【c++】29.设计模式总结
- Java NIO学习系列六:Java中的IO模型
- 程序员面试题精选100题(27)-二元树的深度[数据结构]
- STL的红与黑--rb_tree红黑树
- 数字图像处理:第二章 图象获取、显示、表示与处理