简体   繁体   English

将文件读入 std::vector<std::byte>

[英]Read file into std::vector<std::byte>

I'm trying to read a file in binary format into a std::vector<std::byte>我正在尝试将二进制格式的文件读入std::vector<std::byte>

  std::ifstream fStream(fName, std::ios::binary);

  std::vector<std::byte> file_content((std::istreambuf_iterator<std::byte>(fStream)),
                                        std::istreambuf_iterator<std::byte>());

but I'm getting this error (which to me looks like istreambuf_iterator is missing an overload for std::byte )但我收到此错误(在我看来istreambuf_iterator缺少std::byte的重载)

error: no matching function for call to ‘std::istreambuf_iterator<std::byte>::istreambuf_iterator(std::ifstream&)’
     std::vector<std::byte> file_content((std::istreambuf_iterator<std::byte>(fStream)),

Am I doing something wrong ?我做错了什么吗? And if yes what is the best way to do this ?如果是的话,最好的方法是什么?

Thanks!谢谢!

I'm trying to read a file in binary format into a std::vector<std::byte>我正在尝试将二进制格式的文件读入std::vector<std::byte>

You are using std::istream_iterator , which reads from an std::istream using operator>> , which performs a formatted read instead of a binary read by default.您正在使用std::istream_iterator ,它使用operator>>std::istream读取,默认情况下执行格式化读取而不是二进制读取。 Use std::istream::read() to read binary data.使用std::istream::read()读取二进制数据。

If you want to use std::istring_iterator to read bytes, you would need to define a custom operator>> that calls std::istream::read() or std::stream::get() .如果要使用std::istring_iterator读取字节,则需要定义调用std::istream::read()std::stream::get()的自定义operator>> But this would be inefficient since it would read 1 byte at a time.但这将是低效的,因为它一次读取 1 个字节。 It is better to call read() directly to read blocks of multiple bytes at a time.最好直接调用read()读取多个字节的块。 For instance, query the file size, preallocate the std::vector to that size, and then read() from the std::ifstream directly into the std::vector for that size.例如,查询文件大小,将std::vector预分配到该大小,然后从std::ifstream直接read()到该大小的std::vector中。

Update : I just noticed that you are using std::istreambuf_iterator instead of std::istream_iterator .更新:我刚刚注意到您使用的是std::istreambuf_iterator而不是std::istream_iterator std::istreambuf_iterator does not use operator>> , so it would be better suited for reading bytes. std::istreambuf_iterator不使用operator>> ,因此它更适合读取字节。 However, it still reads 1 byte at a time, so what I said about using std::istream::read() to read multiple bytes at a time still applies.但是,它仍然一次读取 1 个字节,所以我所说的使用std::istream::read()读取多个字节仍然适用。

you should be able to do it like this:你应该可以这样做:

  std::basic_ifstream<std::byte> fStream{fName, std::ios::binary};

  std::vector<std::byte> file_content{ std::istreambuf_iterator<std::byte>(fStream), {} };

unfortunately only char iterators are implemented in STL , so不幸的是,在STL 中只实现了字符迭代器,所以

std::istreambuf_iterator<std::byte>

will cause an error.会导致错误。 so use所以用

std::istreambuf_iterator<char>

instead.相反。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM