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;

}

相关推荐
闲猫6 分钟前
Python 虚拟环境 virtualenv & uvicorn 服务搭建 & FAstAPI 使用
开发语言·python
三月不知肉味y32 分钟前
2026-07-05-JAVA面试场景题训练
java·开发语言·面试
豆瓣鸡1 小时前
封装 API 请求日志切面——从注解定义到 Starter 自动装配
java·开发语言·spring boot
༄沐࿆风࿆࿆2 小时前
JavaScript函数完全指南:从基础语法到闭包原理与应用实战
开发语言·javascript
KobeSacre3 小时前
DelayQueue 源码
java·开发语言
Let's Chat Coding3 小时前
身份识别、身份认证与授权的区别
c++
han_hanker3 小时前
重新认识枚举enum
开发语言
鹏易灵4 小时前
C++——8.移动语义初探(移动构造、移动赋值)
开发语言·c++·php
2601_962341304 小时前
计算机毕业设计之jsp考研在线复习平台
java·大数据·开发语言·hadoop·python·考研·课程设计
凯瑟琳.奥古斯特4 小时前
力扣1008:前序重建BST
开发语言·c++·算法·leetcode·职场和发展