工具类库系列(十四)-FileData
来源:互联网 发布:java 手动回收垃圾 编辑:程序博客网 时间:2024/06/05 06:52
第十四个工具类:FileData
用于文本文件读取/保存,以及在内存中修改文本文件内容。
读取:ifstream
保存:ofstream
修改内容:以string类型,获取/设置,文本文件内容
最大支持文件大小1M
上代码:
FileData.h
#ifndef __FileData_h__#define __FileData_h__#include <string>namespace common{namespace tool{class FileData{public:FileData();FileData(const FileData& other);~FileData();FileData& operator=(const FileData& other);// 提取文本文件内容,将文本中内容下标为[begin, end]之间(包括)的字符,提取到data中void GetData(std::string& data, unsigned int begin, unsigned int end);// 设置文本文件内容,用data中内容,替换掉文本中内容下标为[begin, end]之间(包括)的字符void SetData(const std::string& data, unsigned int begin, unsigned int end);// 获取文本文件内容长度unsigned int GetLen();// 设置文本文件内容长度void SetLen(unsigned int len);// 读取/保存文本文件void Load(const std::string& fileName);void Save(const std::string& fileName);// 文本数据最大支持1Mstatic const unsigned int MaxDataLen = 1024 * 1024;private:unsigned int m_DataLen;char m_Data[MaxDataLen];};}}#endif
FileData.cpp
#include "FileData.h"#include <string.h>#include <fstream>namespace common{namespace tool{FileData::FileData(){m_DataLen = 0;}FileData::FileData(const FileData& other){*this = other;}FileData::~FileData(){}FileData& FileData::operator=(const FileData& other){m_DataLen = other.m_DataLen;memcpy(m_Data, other.m_Data, other.m_DataLen);return *this;}void FileData::GetData(std::string& data, unsigned int begin, unsigned int end){if (begin <= end && end < MaxDataLen){data.assign(m_Data + begin, end - begin + 1);}}void FileData::SetData(const std::string& data, unsigned int begin, unsigned int end){if (begin <= end && end < MaxDataLen && (end - begin + 1) <= data.length()){memcpy(m_Data + begin, data.c_str(), end - begin + 1);if (m_DataLen < end + 1){m_DataLen = end + 1;}}}unsigned int FileData::GetLen(){return m_DataLen;}void FileData::SetLen(unsigned int len){m_DataLen = len;}void FileData::Load(const std::string& fileName){std::ifstream file;file.open(fileName.c_str(), std::ios::in | std::ios::binary | std::ios::ate);if (file){m_DataLen = static_cast<unsigned int>(file.tellg());if (m_DataLen <= MaxDataLen){file.seekg(0, std::ios::beg);file.read(m_Data, m_DataLen);}file.close();}}void FileData::Save(const std::string& fileName){std::ofstream file;file.open(fileName.c_str(), std::ios::out | std::ios::trunc | std::ios::binary);if (file){file.write(m_Data, m_DataLen);file.flush();file.close();}}}}
0 0
- 工具类库系列(十四)-FileData
- 一起学DNS系列(十四)DNS查询工具之DIG的使用(1)
- 一起学DNS系列(十四)DNS查询工具之DIG的使用(2)
- jQuery-1.9.1源码分析系列(十四) 一些jQuery工具
- 【Python学习系列十四】IPython命令行式交互界面工具Jupyter
- 工具类库系列(一)-StringTool
- 工具类库系列(二)-ExePath
- 工具类库系列(三)-IniReader
- 工具类库系列(四)-CsvReader
- 工具类库系列(五)-Timer
- 工具类库系列(六)-TimeTool
- 工具类库系列(七)-Logger
- 工具类库系列(八)-WinService
- 工具类库系列(九)-ReflectEnum
- 工具类库系列(十)-Object
- 工具类库系列(十一)-ObjectMap
- 工具类库系列(十二)-ObjectSharedPtrMap
- 工具类库系列(十三)-ObjectSharedPtrMap2
- 自学iOS开发系列----UI(视图编程入门:UIGestureRecognizer)
- Retrofit学习使用中犯的错
- leetcode_middle_85_33. Search in Rotated Sorted Array
- Learn PHP第三章:operator2(赋值运算符)
- 使用Eclipse构建Maven项目 (step-by-step)
- 工具类库系列(十四)-FileData
- 进程内存地址随机化--与位置无关
- HashSet和HashMap的区别比较
- app项目---"西安天气"
- android webview加载String类型html
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(六)maven整合SSM
- Android Broadcast IP拨号器
- Xcode自定义app内部导航栏
- Java NIO