Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read file into std::vector<std::byte>

Tags:

c++

c++17

I'm trying to read a file in binary format into a 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)

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!

like image 291
Théo Champion Avatar asked Feb 24 '18 16:02

Théo Champion


2 Answers

I'm trying to read a file in binary format into a 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. Use std::istream::read() to read binary data.

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(). But this would be inefficient since it would read 1 byte at a time. It is better to call read() directly to read blocks of multiple bytes at a time. 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.

Update: I just noticed that you are using std::istreambuf_iterator instead of std::istream_iterator. std::istreambuf_iterator does not use operator>>, so it would be better suited for reading bytes. 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.

like image 104
Remy Lebeau Avatar answered Oct 20 '22 15:10

Remy Lebeau


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), {} };
like image 33
randomguy Avatar answered Oct 20 '22 16:10

randomguy