I have the following code which I have been using on a 188 byte file:
std::ifstream is("filename", std::ios::binary);
std::vector<uint8_t> buffer;
std::istream_iterator<uint8_t> i_input(is);
std::copy(i_input, std::istream_iterator<uint8_t>(),
std::back_inserter(buffer));
std::cout << buffer.size();
However it is only reading 186 bytes of the 188 bytes.
I have confirmed the file size in a hexeditor as well as with ls -al
.
I don't know why, but by default that seems to skip whitespace. You need to disable that with noskipws
:
is >> std::noskipws;
What are the last two bytes? Also, you don't really need a istream_iterator
for reading binary data like this. That's overkill and probably slower than using streambuf
.
See this example from wilhelmtell's great answer:
#include<iterator>
// ...
std::ifstream testFile("testfile", std::ios::in | std::ios::binary);
std::vector<char> fileContents((std::istreambuf_iterator<char>(testFile)),
std::istreambuf_iterator<char>());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With