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;

}

相关推荐
云泽80828 分钟前
从零搭建 WSL2 + Ubuntu C/C++ 开发环境:原理、实践与底层架构解析
linux·c++·windows·ubuntu·wsl2
初願致夕霞7 小时前
C++11:类型推导与完美转发复盘笔记
c++
小猪写代码7 小时前
C++中什么是类,什么是对象
c++
君顾110 小时前
上海24小时自助健身房系统开发实战指南:从架构设计到落地部署
java·开发语言·健身房
香菜TTT10 小时前
大模型上下文协议(MCP):AI 应用的“USB-C”接口技术
开发语言·人工智能·经验分享
钓鱼的肝11 小时前
梳理(1-5)
c++·经验分享·笔记·算法·青少年编程
aramae11 小时前
模拟实现strlen()函数 (C语言)
c语言·开发语言·后端
jimy112 小时前
readelf 查看“派生类”的vtable符号
开发语言·c++
蒸蒸yyyyzwd12 小时前
cpp 选手秋招学习笔记 day37 面经学习
c++·八股
ctlover12 小时前
排序与查找算法详解
开发语言·python