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;

}

相关推荐
周杰伦的稻香24 分钟前
Go + Redis:本地部署高性能图片主色调提取服务
开发语言·redis·golang
吴梓穆29 分钟前
Python 语法基础 函数
开发语言·python
不负岁月无痕32 分钟前
C++ 模板核心内容与高频面试题汇总
java·开发语言·c++
Kobebryant-Manba37 分钟前
学习文本处理
开发语言·python
福大大架构师每日一题1 小时前
2026年6月TIOBE编程语言排行榜,Go语言排名第13,Rust语言排名12。关于Rust已进入平台期的报道似乎为时过早。
开发语言·golang·rust
无限进步_1 小时前
从零实现一个迷你Shell——深入理解Linux命令行解释器
linux·运维·服务器·开发语言·c++·chrome
拙慕JULY1 小时前
小程序返回 base64 文件报错
开发语言·javascript·小程序
月疯1 小时前
torch:expand和repeate的区别
开发语言·python·深度学习
Drone_xjw1 小时前
qt配置项目样式表
开发语言·qt
devilnumber1 小时前
静态代理 & 动态代理:实战运用 + 场景区别 + 怎么选
java·开发语言·代理模式