istream_iterator to iterate through bytes in a binary file

后端 未结 2 1020
攒了一身酷
攒了一身酷 2021-01-13 11:48

Given a file containing the following hex code: 0B 00 00 00 00 00 20 41

I\'m trying to populate an std::vector and then check

2条回答
  •  日久生厌
    2021-01-13 12:28

    Don't use std::istream_iterator: 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 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.

提交回复
热议问题