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;

}

相关推荐
haolin123.21 分钟前
STL vector底层揭秘:从构造到迭代器失效
开发语言·c++·算法
大模型码小白23 分钟前
ChatGPT 的回答也能被结构化抓取?AI Bot Scraper 对比测评
服务器·开发语言·数据库·人工智能·spring·chatgpt
尘中远28 分钟前
给C++工业软件搭建 Agent
开发语言·c++·qt·ai·agent
2401_8685347836 分钟前
网规备考_2.4 路由协议
c++·设计模式
啦啦啦啦啦zzzz42 分钟前
自定义日志缓冲区(上)c++20
linux·服务器·c++·网络编程
wabs66644 分钟前
关于二叉树【力扣199.二叉树的右视图的思考】
数据结构·c++·算法·leetcode·二叉树·层序遍历
三8441 小时前
redis三大机制:配置、持久化、主从复制(getshell 原理地基)
开发语言·php
hope_wisdom1 小时前
C/C++数据结构之二叉树的遍历
c语言·数据结构·c++·二叉树·深度优先·广度优先
万年咸鱼1 小时前
Java PrintStream 详解:从基础用法到实战技巧
java·开发语言·python
guwentian1 小时前
手撕 MCP:用 TypeScript 从零写一个能跑的最小客户端(附可运行 demo)
开发语言·nodejs·mcp