C++文件读写的封装

在C++开发中,文件读写是很常用的功能,出于代码复用的考虑,将它们进行封装。

其中,默认读写的文件是文本文件,并且需要按行处理。调用者只需要关注读取出来的每一行的处理方式。写文件只是简单的开关文件流。

具体代码如下:

 /**
* @brief 读文件
* @author env0y
* @param [in] const std::string& filename,文件名(绝对路径)
[in] T& fn,对读出的数据进行逐行操作的函数.fn(std::string,...)
[in/out] std::map<std::string,int>& fieldIdx,表头字段及其索引值
[in] const char Separator = ',',表头字段分隔符,默认','
* @param [out] none
* @return bool
* @note 1. 参数fieldIdx带入表头字段值,带出字段值对应的索引
2. 参数fn的第一个参数必须是std::string类型
*/
template<typename T>
static inline bool ReadFile(const std::string& filename, T& fn, std::map<std::string,int>& fieldIdx,const char Separator = ',' )
{
typedef std::map<std::string,int> FieldMap;
ifstream fd(filename,std::ios::in);
fd.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t>));
if (fd.fail())
{
//LOG_ERROR_F( ModuleInfo,_T("ReadFile:Open [%s] failure."),CString(filename.c_str()).GetString());
return false;
}
auto Raii = [&](){ fd.close(); };
ON_SCOPE_EXIT(Raii);
//LOG_DEBUG_F( ModuleInfo,_T("ReadFile:file [%s] size [%lld] Bytes."),CString(filename.c_str()).GetString(),CalcFileSz(fd)); std::string oneline;
(void)getline(fd,oneline);
if ( false == FieldIndexLookup(oneline,fieldIdx,Separator) )
{
return false;
} do
{
(void)fn( oneline ); //每读一行,传递给用户定义的函数
oneline.clear();
(void)getline(fd,oneline); } while ( !fd.eof() ); return true;
} /**
* @brief 字段索引查找确认
* @author env0y [2015/12/24 11:09:49]
* @param [in] const std::string& tableHeader,表头
[in/out] std::map<std::string,int>& fieldIdx,字段为入参,索引为出参
char separator = ',',表头字段分隔符,默认为','
* @param [out] none
* @return bool,查找是否成功
* @note none
*/
static inline bool FieldIndexLookup( const std::string& tableHeader,std::map<std::string,int>& fieldIdx,char separator /*= ','*/ )
{
if (fieldIdx.empty())
{
return true;
}
typedef std::map<std::string,int> FieldMap;
const int FieldNotExist = -1;
(void)std::for_each( fieldIdx.begin(),fieldIdx.end(),[=](FieldMap::value_type& ele){ ele.second = FieldNotExist;});
int TmpIdx = FieldNotExist;
std::string subStr = tableHeader;
string::size_type curpos = std::string::npos;
do
{
curpos = subStr.find_first_of(separator);
std::string fieldStr = subStr.substr(0,curpos);
subStr = subStr.substr(curpos+1); ++TmpIdx; auto it = fieldIdx.find( fieldStr );
if ( it != fieldIdx.end())
{
it->second = TmpIdx;
}
} while ( std::string::npos != curpos ); bool bFieldsInsufficient(false);//- 字段是否缺失
(void)std::for_each(fieldIdx.begin(),fieldIdx.end(),[&](const FieldMap::value_type& ele)
{
if ( ele.second == FieldNotExist )
{
bFieldsInsufficient = true;
std::cout << "FieldIndex: Lack of field:" << ele.first.c_str() << "\n";
//LOG_ERROR_F( ModuleInfo,_T("FieldIndex: Lack of field [%s]."),CString(ele.first.c_str()).GetString() );
}
});
return (false == bFieldsInsufficient);
}

  函数FiledIndexLookup用来查找指定表头是否存在于第一行文件中。

写文件的代码如下:

/**
* @brief 计算文件大小
* @author env0y [2015/10/19 17:25:37]
* @param [in] fstream& fd,文件描述符
* @param [out] none
* @return unsigned __int64,文件大小 单位字节
* @note none
*/
template<typename FileStream>
static inline unsigned __int64 CalcFileSz(FileStream& fd)
{
(void)fd.seekg(0,ios::end);
streampos uSz = fd.tellg();
(void)fd.seekg(0,ios::beg);
return (unsigned __int64)uSz;
} /**
* @brief 写文件
* @author env0y [2015/10/19 9:14:33]
* @param [in] const std::string& filename,要生成的文件(绝对路径)
[in] T& fn,函数指针/成员函数/函数对象/仿函数/Lambda表达式
[in] int mode = std::ios::out,文件操作模式,默认ios::out
[in] const std::string & title = "",生成的文件表头,默认空
* @param [out] none
* @return bool
* @note fn的参数(wofstream&,...)
*/
template<typename T>
static inline bool WriteFile(const std::string& filename,T& fn,int mode = std::ios::out,const std::string & title = "")
{
(void)SHCreateDirectoryEx(nullptr, CString(filename.substr(0,filename.find_last_of("\\")).c_str()) ,nullptr);
//LOG_DEBUG_F( ModuleInfo,_T("output [%s]."),CString(filename.c_str()).GetString());
wofstream ofd(filename,mode);
// ofd.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t>));
if( ofd.fail() )
{
//LOG_ERROR_F( ModuleInfo,_T("WriteFile:Open or Create [%s] failure."),CString(filename.c_str()).GetString());
return false;
}
if( title.length() )
{
title.back() == '\n' ? ofd << title.c_str() : ofd << title.c_str() << "\n";
} (void)fn(ofd); ofd.close(); return true;
} template<typename T,size_t X>
static inline bool WriteFile(const std::string(&filename)[X],T& fn,int mode = std::ios::out,const std::string & title = "")
{
std::wofstream arrofds[X];
size_t ulCnt = 0;
bool bRst = std::any_of(std::begin(filename),std::end(filename),[&](const std::string& name)->bool
{
(void)SHCreateDirectoryEx(nullptr, CString(name.substr(0,name.find_last_of("\\")).c_str()) ,nullptr);
arrofds[ulCnt].open(name,mode);
if( arrofds[ulCnt].fail() )
{
LOG_ERROR_F( ModuleInfo,_T("WriteFile:Open or Create [%s] failure."),CString(name.c_str()).GetString());
return true;
}
arrofds[ulCnt++] << title.c_str();
return false;
});
if (bRst)
{
return false;
} (void)fn(arrofds);
std::for_each(std::begin(arrofds),std::end(arrofds),[](std::wofstream& fd)
{
fd.close();
}); return true;
}

  第一个是一次写一个文件,第二个是写多个。

调用举例如下:

bool COutputPDFGridULSINRFreqBand::LoadCurrentBHGridInfo()
{
CString CurrentBHGridInfoPath = JoinPath(m_prjPath, CString(_T("GridLevel\\Total\\CurrentBHGridInfo.csv")));
m_FileHeadRel.emplace(std::make_pair("GridID",-1));
m_FileHeadRel.emplace(std::make_pair("Longitude",-1));
m_FileHeadRel.emplace(std::make_pair("Latitude",-1));
m_FileHeadRel.emplace(std::make_pair("Site ID",-1));
m_FileHeadRel.emplace(std::make_pair("Cell ID",-1));
m_FileHeadRel.emplace(std::make_pair("FrequencyBand",-1));
m_FileHeadRel.emplace(std::make_pair("BandNum",-1));
m_FileHeadRel.emplace(std::make_pair("CellULSINR",-1));
m_FileHeadRel.emplace(std::make_pair("CellPrivateUEMRNum",-1));
return ReadFile(CString2String(CurrentBHGridInfoPath),std::bind(&COutputPDFGridULSINRFreqBand::ReadLine,this,std::placeholders::_1),m_FileHeadRel);
}

第二个参数是模板参数,支持lambda表达式、c全局函数、c++成员函数等。

在实际项目中可以把它们封装到一个命名空间,可以更安全方便的使用。 :)

除零保护也是c++经常做的事情,可以提取成如下函数:

/**
* @brief 除法保护
* @author env0y
* @param [in] Dividend dividend : 分子
[in] Divisor divisor : 分母
* @param [out] none
* @return : 如果分母为0(除数为0),返回0; 否则返回正常除数值
*/
template <typename Dividend,typename Divisor>
static inline double DivisionProtection(Dividend dividend,Divisor divisor)
{
double dResult(0.0);
static const char* Integers[] =
{
"char", "unsigned char",
"short", "unsigned short",
"int", "unsigned int",
"long", "unsigned long",
"__int64", "unsigned __int64",
"long long","unsigned long long"
};
static const char* Floats[] = {"float","double"}; static const int iSz = _countof(Integers);
static const int fSz = _countof(Floats); const char* typeDivisor = typeid(divisor).name(); auto fCmp = [&](const char* str){ return 0 == strcmp(str,typeDivisor);}; if ( (Floats+fSz) != std::find_if(Floats,Floats+fSz,fCmp) )
{
bool bIsZero = (fabs((double)divisor) < 1e-15 ) || (fabs((float)divisor) < 1e-6 );
dResult = bIsZero ? 0.0 : dividend/divisor ;
}
else if ( (Integers+iSz) != std::find_if(Integers,Integers+iSz,fCmp) )
{
dResult = (0 == divisor) ? 0 : 1.0*dividend/divisor;
} return dResult;
}

  

而对浮点型数四舍五入函数则可以使用下面方法:

/**
* @brief 根据所需精度四舍五入
* @author env0y [2015/8/29]
* @param [in] const double& x,原始值
[in] int precision,精度值
* @param [out] none
* @return double,四舍五入之后的值
* @note 默认精度为1
*/
static inline double RoundByPrecision(const double& x, int precision/* = 1*/)
{
if ( precision < 0 )
{
return x;
}
double temp = x * pow((double)10, precision);
temp = floor(temp + 0.5);
return (temp * pow((double)10, -precision));
}

  

我们知道c++11已经有std::hash方法,那如何哈希一个std::pair呢?

可以像下面这样:

template <class T>
inline void hash_combine(std::size_t & seed, const T & v)
{
std::hash<T> hasher;
seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
} namespace std
{
template<typename S, typename T> struct hash<pair<S, T>>
{
inline size_t operator()(const pair<S, T> & v) const
{
size_t seed = 0;
::hash_combine(seed, v.first);
::hash_combine(seed, v.second);
return seed;
}
}; }

而像std::wstring和std::string之间的相互转化,则可以:

static inline std::string ws2s( const std::wstring& wstr )
{
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converterX;
return converterX.to_bytes(wstr);
} static inline std::wstring s2ws( const std::string& str )
{
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converterX;
return converterX.from_bytes(str);
}

  

写到这里发现,读文件那里用到了RAII,所以把宏定义补充如下:

class ScopeGuard
{
public:
explicit ScopeGuard(std::function<void()> onExitScope):onExitScope_(onExitScope){};
~ScopeGuard(){onExitScope_();};
private:
std::function<void()> onExitScope_;
// noncopyable
private:
ScopeGuard(ScopeGuard const&);
ScopeGuard& operator=(ScopeGuard const&);
};
/*lint -restore*/ #define SCOPEGUARD_LINENAME_CAT(name, line) name##line
#define SCOPEGUARD_LINENAME(name, line) SCOPEGUARD_LINENAME_CAT(name, line)
//#define ON_SCOPE_EXIT(callback) ScopeGuard<decltype(callback)> SCOPEGUARD_LINENAME(EXIT, __LINE__)(callback)
#define ON_SCOPE_EXIT(callback) ScopeGuard SCOPEGUARD_LINENAME(EXIT, __LINE__)(callback)

  

就这些。以后再补充吧!;)

c++常用功能封装的更多相关文章

  1. js实现第一次打开网页弹出指定窗口(常用功能封装很好用)

    js实现第一次打开网页弹出指定窗口(常用功能封装很好用) 一.总结 1.常用功能封装:之前封装的cookie的操作函数非常好用,我自己也可以这么搞 二.js实现第一次打开网页弹出指定窗口 练习1:第一 ...

  2. cocos2D-X LUA 常用功能封装和工作经验的一些解决方案

    --[[ Packaging_KernelEngine.h 文件说明:所有对象在建立时位置是优先的,传入位置参数必须cc.p(X,Y) CurObj:表示要传入当前的对象 将3.10 lua api ...

  3. cocos2D-X 常用功能封装

    Packaging_Kernel.h #pragma once #include <string> #include <map> #include <vector> ...

  4. echarts常用功能封装|抽象为mixin

    目前已解锁以下功能: [x] 初始化echarts(initChart) [x] 获取echarts参数配置(getOption) [x] 生成echarts图表(setOption) [x] 监听r ...

  5. php一些常用功能封装

    //二分查找 function bin_sch($array, $low, $high, $k) { if ($low <= $high) { $mid = intval(($low + $hi ...

  6. EXCEL2016 OLE/COM开发-常用功能封装代码

    hpp #pragma once #include "stdafx.h" #include "CApplication.h" #include "CW ...

  7. JavaScript 常用功能总结

    小编吐血整理加上翻译,太辛苦了~求赞! 本文主要总结了JavaScript 常用功能总结,如一些常用的JS 对象,基本数据结构,功能函数等,还有一些常用的设计模式. 目录: 众所周知,JavaScri ...

  8. Android android-common 常用功能和工具集合

    本文内容 环境 android-common 项目结构 演示 android-common 参考资料 android-common 主要包括如下内容: 缓存,包括图片缓存.预取缓存.网络缓存. 公共 ...

  9. 开源三维地球GIS引擎Cesium常用功能的开发

    Cesium是一个非常优秀的三维地球GIS引擎(开源且免费).能够加载各种符合标准的地图图层,瓦片图.矢量图等都支持.支持3DMax等建模软件生成的obj文件,支持通用的GIS计算:支持DEM高程图. ...

随机推荐

  1. Labview学习笔记(一)

    一.概述 Labview是一种工业标准图形化编程工具,主要用于开发测试.测量与控制系统,拥有一个可以完成任何编程任务的庞大函数库,包括数据采集.GPIB.串口控制.数据分析.数据显示及数据存储等. L ...

  2. jmeter3.1 压测

    压测目标:error 为0,线程起到250,服务器配置达到最大 一.Jmeter3.1 压测 JMeter3.1提供一个用于生成HTML页面格式图形化报告的扩展模块.该模块支持通过两种方式生成多维度图 ...

  3. swift-UITableView的基本使用

    废话不多说了,直接贴我今天写的代码吧:如果新手有什么不懂的,可以发我邮箱. // //  singleInfo.swift            个人信息 //  Housekeeper // //  ...

  4. lua返回页面时中文乱码

    1.在nginx.conf文件中的server标签里添加charset utf-8; 2.查看lua文件编码是否为utf-8

  5. 快速上手Linux 玩转典型应用_慕课网笔记

    1.没有exe安装程序 2.区分大小写 3.一切皆文件 4.文件后缀不是那么重要,只是为了好识别 -------------------------------------------------- ...

  6. 【例题4-3 uva 133】The Dole Queue

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 写个数组模拟链表 但注意,得用个辅助数组flag.. 不然可能会出现没能跳过中间的被占区域的情况. 比如 1 2 idx # # # ...

  7. dubbo-dubboAdmin安装(一)

    简介 Dubbo是什么? dubbo是阿里开源的分布式服务治理框架,对服务的负载均衡,权重,监控,路由规则,禁用启用的管理,以及服务的自动注册和发现 分布式架构下面临问题 在分布式架构下,我们会将一个 ...

  8. 关于Excel导入导出POI工具类

    import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import ...

  9. PHP扩展开发--实验成功

    原文:http://kimi.it/496.html http://blog.csdn.net/u011957758/article/details/72234075 ---------------- ...

  10. Cocos2d-x3.0RC2 EditBox

    EditBox样例 将例如以下代码拷贝到新建的project中就能看到效果. HelloWorldScene.h #include "cocos2d.h" /*这里要引入头文件*/ ...