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;

}

相关推荐
编程小Y3 分钟前
Bash 替换机制
开发语言·chrome·bash
我要学脑机5 分钟前
一个图谱映射到功能网络yeo7或17的解决方案
开发语言·网络·php
坐吃山猪12 分钟前
Python之PDF小工具
开发语言·python·pdf
代码栈上的思考13 分钟前
MyBatis——动态SQL讲解
java·开发语言·数据库
王柏龙17 分钟前
c# aggregate使用
开发语言·c#
小鸡吃米…17 分钟前
Python - 构造函数
开发语言·python
moonquakeTT18 分钟前
C++:智能指针
开发语言·c++
hoiii18719 分钟前
基于MATLAB实现无监督数据建模
开发语言·matlab
Lenyiin20 分钟前
第 97 场周赛:公平的糖果交换、查找和替换模式、根据前序和后序遍历构造二叉树、子序列宽度之和
java·c++·python·leetcode·周赛·lenyiin
Clarence Liu24 分钟前
MacOS 在Trae IDE中构建现代C++开发环境:从新手到高效的完整指南
c++·ide·macos