c++ ofstream 和ifstream 读写二进制文件的简单操作

主要使用ofstream 和ifstream 的技术 纯c++实现

有时间感觉使用qt的文件操作没有c++的适用 特别是遇到中文或者\0 的特殊字符时 还是c++有效

void WriteFile(const string & sFilePath, const string & sContent)

{

ofstream out(sFilePath.c_str(), ios::binary);

if (out.is_open())

{

out.write(sContent.c_str(), sContent.size());

out.close();

}

return;

}

void ReadFile(const string & sFile, string & sContent)

{

ifstream in(sFile, ios::binary);

if (!in.is_open())

{

cout << "Error opening file"; exit(1);

}

in.seekg(0, ios::end);

int isize = in.tellg();

in.seekg(0, ios::beg);

unsigned char *pBuf = new unsigned char[isize];

in.read((char *)pBuf, isize);

copy(pBuf, pBuf + isize, back_inserter(sContent));

if (nullptr != pBuf)

{

delete [] pBuf;

pBuf = nullptr;

}

return;

}

相关推荐
星辰_mya几秒前
ThreadLocal 与内存泄漏
java·开发语言
Data_Journal2 分钟前
如何将网站数据抓取到 Excel:一步步指南
大数据·开发语言·数据库·人工智能·php
十年编程老舅4 分钟前
C++ 原子操作实战:实现无锁数据结构
linux·c++·c++11·原子操作·无锁队列
米码收割机9 分钟前
【AI】OpenClaw问题排查
开发语言·数据库·c++·python
¿i?9 分钟前
LinkedList 含iterator写法的理解
java·开发语言
所谓伊人,在水一方33310 分钟前
【Python数据科学实战之路】第10章 | 机器学习基础:从理论到实践的完整入门
开发语言·人工智能·python·机器学习·matplotlib
无风听海11 分钟前
Python之TypeVar深入解析
开发语言·python·typevar
白中白1213813 分钟前
杂七杂八补充系列
开发语言·前端·javascript
佩奇大王15 分钟前
P8 单词分析
java·开发语言
飞Link16 分钟前
概率图模型的基石:隐马可夫模型 (HMM) 深度解析
开发语言·python·算法