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;

}

相关推荐
bing.shao几秒前
Golang中实现基于角色的访问控制(RBAC)
开发语言·后端·golang
shenzhenNBA几秒前
如何在python项目中使用日志功能?通用版本
java·开发语言·python·日志·log
why151几秒前
面经整理——Go
开发语言·后端·golang
weixin_30777913几秒前
简化多维度测试:Jenkins Matrix Project 的核心概念与最佳实践
运维·开发语言·架构·jenkins
羑悻的小杀马特5 分钟前
C++与Redis高效交互:通过optional与迭代器玩转String/List/Set......,打造高性能存储方案!
c++·redis·交互
weixin_307779136 分钟前
Jenkins Matrix Authorization Strategy插件:详解与应用指南
运维·开发语言·架构·jenkins
通往曙光的路上9 分钟前
异步任务la
java·开发语言
星释9 分钟前
Rust 练习册 116:杂志剪贴侦探游戏
开发语言·后端·rust
+++.19 分钟前
c++雪花屏(vsCode+cmake+mingw+ninja)
开发语言·c++·vscode
小年糕是糕手24 分钟前
【C++】内存管理(下)
java·c语言·开发语言·数据结构·c++·算法