从文件逐字节读取 [英] read byte by byte from a file

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

问题描述

如何以字节形式读取文件,而不使用向量。

How can I read a file in form of bytes, not using a vector.

通过使用它,我可以将整个文件读入一个Bytes的向量。

By using this, I can read the entire file into a vector of Bytes.

std::basic_ifstream<BYTE> file(driveName, std::ios::binary);
vector<BYTE> x = std::vector<BYTE>(
    (std::istreambuf_iterator<BYTE>(file)),
     std::istreambuf_iterator<BYTE>() );


$ b 。,
resize选项不工作在这里。

But, I want to read 512 bytes first, then 'x' bytes, 'x1' bytes etc., The resize option doesnt work here.

我看到这个连结,将二进制文件读入unsigned chars的向量,这会造成更多的混乱。

I saw this link, reading the binary file into the vector of unsigned chars, which created more confusion.

任何帮助, 。

推荐答案

您可以使用下层界面:

std::ifstream ifs(filename, std::ios::binary);
char buf1[512];
ifs.read(buf1, sizeof(buf1) / sizeof(*buf1));
char buf2[x];
ifs.read(buf2, sizeof(buf2) / sizeof(*buf2));
char buf3[x1];
ifs.read(buf3, sizeof(buf3) / sizeof(*buf3));

只需检查EOF和错误。

Just check for EOF and errors.

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

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