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;

}

相关推荐
ESDWAN6 分钟前
跨境电商网络专线怎么选?从带宽、延迟到SD-WAN部署与带宽管理的完整落地指南
开发语言·网络·php
web打印社区11 分钟前
Lodop 提示未安装或请升级:Chrome 里先分清该装哪套
开发语言·前端·javascript·chrome·websocket·http
shangan_325 分钟前
C语言学习
c语言·开发语言·学习
重生之小比特26 分钟前
【C++进阶】02 Linux基本指令
java·数据库·c++
栈溢出了27 分钟前
LangGraph State 学习笔记
开发语言·人工智能·python
HEJOO929 分钟前
Go 语言结构体定义与实例详解
开发语言·算法·golang
(Charon)31 分钟前
【C++面试】手写智能指针(一):从unique_ptr理解独占所有权与移动语义
开发语言·c++
李游Leo35 分钟前
HarmonyOS 7 + Spatial Recon Kit-C++:3DGS 输入帧内参与位姿的版本一致性门禁【鸿蒙心迹】
c++·3d·harmonyos
Brilliantwxx37 分钟前
【STM32】 DMA 源码分析与实战(下):DMA 收发状态机与 HAL 分层
android·开发语言·stm32·单片机·嵌入式硬件·mcu
(Charon)1 小时前
【C++面试】手写智能指针(二):从shared_ptr理解引用计数与共享所有权
c++·算法