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 char[isize];

in.read((char *)pBuf, isize);

copy(pBuf, pBuf + isize, back_inserter(sContent));

if (nullptr != pBuf)

{

delete [] pBuf;

pBuf = nullptr;

}

return;

}

相关推荐
宋恩淇要努力1 小时前
C++继承
开发语言·c++
沿着路走到底2 小时前
python 基础
开发语言·python
沐知全栈开发3 小时前
C# 委托(Delegate)
开发语言
江公望3 小时前
Qt qmlRegisterSingletonType()函数浅谈
c++·qt
任子菲阳3 小时前
学Java第三十四天-----抽象类和抽象方法
java·开发语言
csbysj20204 小时前
如何使用 XML Schema
开发语言
R6bandito_4 小时前
STM32中printf的重定向详解
开发语言·经验分享·stm32·单片机·嵌入式硬件·mcu
逆小舟4 小时前
【C/C++】指针
c语言·c++·笔记·学习
earthzhang20214 小时前
【1007】计算(a+b)×c的值
c语言·开发语言·数据结构·算法·青少年编程
江公望4 小时前
Qt QtConcurrent使用入门浅解
c++·qt·qml