从文件读取和写入字节(C ++) [英] Read and write bytes from a file (c++)

查看:201
本文介绍了从文件读取和写入字节(C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我可能必须使用fstream对象,但是我不确定如何使用.本质上,我想将文件读入字节缓冲区,对其进行修改,然后将这些字节重写为文件.所以我只需要知道如何做字节I/O.

I think I probably have to use an fstream object but i'm not sure how. Essentially I want to read in a file into a byte buffer, modify it, then rewrite these bytes to a file. So I just need to know how to do byte i/o.

推荐答案

#include <fstream>

ifstream fileBuffer("input file path", ios::in|ios::binary);
ofstream outputBuffer("output file path", ios::out|ios::binary);
char input[1024];
char output[1024];

if (fileBuffer.is_open())
{
    fileBuffer.seekg(0, ios::beg);
    fileBuffer.getline(input, 1024);
}

// Modify output here.

outputBuffer.write(output, sizeof(output));

outputBuffer.close();
fileBuffer.close();

从内存来看,我认为这是怎么回事.

From memory I think this is how it goes.

这篇关于从文件读取和写入字节(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆