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;

}

相关推荐
mit6.8242 分钟前
[vroom] docs | 输入与问题定义 | 任务与运输工具 | json
c++·自动驾驶
Heartoxx2 分钟前
c语言-指针与一维数组
c语言·开发语言·算法
hqxstudying4 分钟前
Java创建型模式---原型模式
java·开发语言·设计模式·代码规范
charlie11451419119 分钟前
如何使用Qt创建一个浮在MainWindow上的滑动小Panel
开发语言·c++·qt·界面设计
神仙别闹27 分钟前
基于Python实现LSTM对股票走势的预测
开发语言·python·lstm
chao_7891 小时前
回溯题解——子集【LeetCode】二进制枚举法
开发语言·数据结构·python·算法·leetcode
cpp_learners3 小时前
QML与C++交互之创建自定义对象
c++·qt·qml
尘世闲鱼3 小时前
解数独(C++版本)
开发语言·c++·算法·解数独
kyle~4 小时前
C/C++字面量
java·c语言·c++
纨妙4 小时前
python打卡day59
开发语言·python