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;

}

相关推荐
Henry Zhu12312 分钟前
C++17 `weak_from_this` 详解:让对象安全地观察自己
c++
鹿角片ljp25 分钟前
Java框架篇:Spring + SpringMVC + SpringBoot + MyBatis深度复习
java·开发语言
xlxxy_2 小时前
外部系统调用SAP接口遇到的一些报错
开发语言·数据库·sap·abap
八角.。2 小时前
方法参数与Debug按键
java·开发语言·jvm
郝亚军2 小时前
FE1.1S-BQFN24BT可以指定IP端口吗?
开发语言·php
阿里嘎多学长2 小时前
2026-08-04 GitHub 热点项目精选
开发语言·程序员·github·代码托管
晴天162 小时前
Rust 快速入门 (从 JavaScript 开发者视角)-Day09
开发语言·javascript·rust
步行cgn3 小时前
carList.forEach(System.out::println)
java·开发语言·mybatis
进击的程序猿~3 小时前
Go 内存分配与垃圾回收源码深度学习手册
开发语言·后端·golang
June`3 小时前
warp shuffle指令
c++·人工智能·算法·cuda