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;

}

相关推荐
kkeeper~1 分钟前
0基础C语言积跬步之深入理解指针(4)
c语言·开发语言
周末也要写八哥18 分钟前
在C++中使用预定义宏
开发语言·c++·算法
Data_Journal27 分钟前
使用Python lxml轻松进行网络爬取
开发语言·php
xcLeigh42 分钟前
IoTDB JDBC 完整使用教程:连接、查询、批处理与字符集配置
开发语言·数据库·qt·iotdb·查询·批处理·连接
学会870上岸华师1 小时前
C 语言程序设计——第一章课后编程题
c语言·开发语言·学习·算法
小小编程路1 小时前
新手快速学 Python 极简速成指南
开发语言·c++·python
rabbit_pro1 小时前
SpringBoot3集成Langchain4j使用Ollama
java·开发语言
宏笋2 小时前
C++ 约束模板参数Concepts详解
c++
计算机安禾2 小时前
【c++面向对象编程】第26篇:对象的内存模型:成员变量与成员函数的存储分离
开发语言·c++·算法
郝学胜-神的一滴2 小时前
Qt 高级开发 005: Qt Creator与Visual Studio 项目双向转换
开发语言·c++·ide·qt·程序人生·visual studio