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;

}

相关推荐
ch.ju4 分钟前
Java Programming Chapter 4——The set-get method.
java·开发语言
dog2504 分钟前
科斯定理与平坦随机网络的自然秩序
开发语言·网络·php
IronMurphy18 分钟前
List 经典问
java·开发语言
Hiter_John24 分钟前
Golang的循环语句
开发语言·算法·golang
绍磊leo31 分钟前
Go 实现类似 FastAPI 的后端服务:从入门到实战
开发语言·golang
一条泥憨鱼32 分钟前
Java网络编程:Socket通信从入门到起飞
java·开发语言·网络·网络编程
hope_wisdom32 分钟前
C/C++数据结构之二叉树基础
c语言·数据结构·c++·二叉树
磊 子33 分钟前
STL算法库讲解1
开发语言·c++·算法
Hiter_John33 分钟前
Golang的条件判断
服务器·开发语言·golang
276695829234 分钟前
谷歌google cookie逆向角度分析
开发语言·python·google·sgss·谷歌搜索·sg-ss·谷歌cookie逆向