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;

}

相关推荐
程序员皮皮林1 分钟前
Dubbo 的 SPI 和 JDK 的 SPI 有什么区别?
java·开发语言·dubbo
啦啦啦啦啦zzzz1 分钟前
数据结构:哈夫曼编码
数据结构·c++·哈夫曼编码
是多巴胺不是尼古丁4 分钟前
java‘期末复习--多态
java·开发语言
兵哥工控9 分钟前
MFC开关量输出发脉冲实例
c++·mfc·开关量发脉冲
ChillCoding16 分钟前
更新中:C++ STL库,查找排序(基础算法),数据结构,数学算法,竞赛相关基础
数据结构·c++·算法
abcy07121317 分钟前
sqlalchemy 原生sql判断条件是否为空,为空则跳过
开发语言·python
智者知已应修善业21 分钟前
【51单片机使用IO组赋值方法实现无源蜂鸣器响时LED12亮不响时34亮】2024-3-7
c++·经验分享·笔记·算法·51单片机
.千余22 分钟前
【C++】深挖STL list底层:解迭代器与节点存储逻辑
开发语言·c++·笔记·学习·其他
淡水瑜24 分钟前
C# 实操
开发语言·c#
雪落漂泊25 分钟前
C++ 继承与多态(上)
开发语言·c++