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;

}

相关推荐
啊阿狸不会拉杆2 分钟前
《计算机网络-自顶向下方法》1.5 协议层次及其服务模型 读书笔记
开发语言·计算机网络·php
tryxr8 分钟前
Chat2Excel 项目通用服务开发
java·开发语言·excel·java项目开发
Jazz_z15 分钟前
使用 Python 合并与取消合并 Excel 单元格
开发语言·python·excel
斑马13923 分钟前
Linux软件编程学习笔记(十)——网络(二)——TCP通信
开发语言·网络·php
多敲代码防脱发26 分钟前
Alibaba Sentinel(熔断、限流)(国内下载Sentinel)
java·开发语言·sentinel
艺杯羹28 分钟前
双轨大模型路由实战:从端侧Ollama边缘推理到云端大模型容灾降级架构深度拆解
开发语言·人工智能·ai·架构·php
quantdash_cc31 分钟前
量化数据管道为什么会出现历史 K 线缺口?从 API 请求到数据质量的完整排查方法
开发语言·python·数据分析·量化·股票数据·quantdash
程与留14 小时前
15_国际化和本地化:tr()、ts 文件、QM 文件、多语言切换
c++·qt
程与留16 小时前
14_Qt 样式表(QSS)入门(语法、选择器、美化实战)
c++·qt