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;

}

相关推荐
Wzx1980121 小时前
python沙箱和docker沙箱你选对了吗?
开发语言·python·docker
牧羊人.3331 小时前
Python 办公自动化从入门到入土|09 数据容器之字典
开发语言·python
小僧景贤2 小时前
嵌入式C语言 第二篇:基础语法|嵌入式C与标准C的核心差异
c语言·开发语言·嵌入式c语言
阿pin2 小时前
Android随笔-AIDL
android·开发语言·aidl
fpcc2 小时前
ubuntu26环境下的开发环境安装处理
c++·并行编程
LiLiYuan.3 小时前
【字符串常量池】
java·开发语言·面试
瑞码空间3 小时前
Python爬虫进阶实战笔记
开发语言·python·计算机·python爬虫
16月6日-晴3 小时前
Java面向对象进阶—static
java·开发语言
爱跳舞的烤冷面3 小时前
自学嵌入式第16天(数据结构篇--链表进阶操作)
开发语言
持敬chijing3 小时前
PHP开发-环境搭建-安装phpstudy-vscode工具
开发语言·vscode·php