Given a file containing the following hex code: 0B 00 00 00 00 00 20 41
I\'m trying to populate an std::vector
istream_iterator
should not be used for reading binary files. It uses operator>>
, which is also not suited for reading binary files (unless those files are of a very specific format which most binary files do not fit). You can use istreambuf_iterator instead. You also want to be sure to open your file in binary mode.
in.open("filepath", std::ios::in | std::ios::binary);
Don't use std::istream_iterator<T>
: that's intended for text formatted input. Most likely it'll skip spaces, for example (you can disable skipping spaces using std::noskipws
, but that's still the wrong thing to do - use std::istreambuf_iterator<char>
instead; the type char
is the character type of the stream).
Also, when processing binary data make sure your stream is opened in binary mode to avoid line end conversions (in case you try that on a platform doing line end conversions). That is, you'd add std::ios_base::binary
to the open mode.