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 charisize;

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

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

if (nullptr != pBuf)

{

delete \[\] pBuf;

pBuf = nullptr;

}

return;

}

相关推荐
weixin_440784116 分钟前
【IntentSeivice实现原理】
android·java·开发语言·intentservice
encoconut14 分钟前
01_C++ 入门基础
c++
岁岁养乐多36 分钟前
Java 序列化相关问题
java·开发语言
CAE二次开发工程师42 分钟前
【C语言入门到精通】-基础篇
c语言·开发语言·python
AAIshangyanxiu1 小时前
涡度通量碳观测数据 MATLAB 全流程解析与敏感性定量分析、观测数据质控、插补及 footprint 建模技术
开发语言·matlab·涡度通量·生态碳汇·生态碳汇模拟
小短腿乄1 小时前
java实现pdf加水印+签名
java·开发语言·pdf
郝学胜-神的一滴2 小时前
中级OpenGL教程 030:gl_FragCoord解锁区域渲染与深度可视化新姿势
c++·unity·游戏引擎·图形渲染·unreal engine·opengl
聆风吟º2 小时前
【金仓数据库征文】Java 应用接入金仓数据库:从驱动、连接池到存储过程调试的踩坑实录
java·开发语言·数据库
不会代码的小猴2 小时前
11. 类和动态内存分配
开发语言·c++
似璟如你3 小时前
Java 开发者的 Go 语法基础:从 0 开始快速上手 Go
java·开发语言·后端·golang·go·编程语言