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;

}

相关推荐
李妍.5 小时前
02Numpy基础(上)
开发语言·python
TheBestRucy5 小时前
Python 九阳神功之贰:面向对象(下)
开发语言·python
吃着火锅x唱着歌6 小时前
Effective C++ 学习笔记 条款43 学习处理模板化基类内的名称
c++·笔记·学习
AI_小站6 小时前
刚面完百度的 Agent 开发岗,我才发现:世界就是个巨大的草台班子
java·开发语言·人工智能·spring·百度·langchain
felixking7 小时前
C++20 协程
开发语言·c++·协程
Zane1994128 小时前
CAS 与原子类:Java 如何实现无锁编程
java·开发语言
码农颜9 小时前
6.3 主函数流程剖析
开发语言·php
TheBestRucy9 小时前
Python 九阳神功:从零筑基到线程飞升
服务器·开发语言·网络·人工智能·python
研☆香10 小时前
聊一聊前端的常见字 关键字
开发语言·前端·javascript
小小龙学IT10 小时前
FlatBuffers 深度解析:Google 开源零拷贝序列化库完全指南
c++·开源