istream_iterator to iterate through bytes in a binary file
Asked Answered
T

2

7

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

I'm trying to populate an std::vector <std::uint8_t> and then checking each byte manually.

Here's the code where I create my vector from two std::istream_iterators using the iterator constructor

using Bytes   = std::vector<std::uint8_t>;
using ByteItr = std::istream_iterator<std::uint8_t>;

Bytes getBytes()
{
    std::ifstream in;
    in.open("filepath");
    in.seekg(0, std::ios::beg);
    Bytes bytes;
    ByteItr start(in);
    ByteItr end;
    return Bytes(start, end);
}

Here's the unit test I'm trying to pass it through:

auto bytes = getBytes();

REQUIRE( bytes.size() == 8 );

CHECK( bytes[0] == 0x0B );
CHECK( bytes[1] == 0x00 );
CHECK( bytes[2] == 0x00 );
CHECK( bytes[3] == 0x00 );
CHECK( bytes[4] == 0x00 );
CHECK( bytes[5] == 0x00 );
CHECK( bytes[6] == 0x20 );
CHECK( bytes[7] == 0x41 );

Why is it that in this context, it skips two elements and implicitly converts my vector of std::uint8_t to unsigned chars?

Thing answered 15/12, 2015 at 22:20 Comment(4)
Could you clarify what do you mean by " it skips two elements and implicitly converts my vector of std::uint8_t to unsigned chars", please ?Roark
Also once you open your file, you could just return {ByteItr{in}, ByteItr{}};, you don't need to seekg to the beginning...Mediacy
@Roark What I mean is that it returns only 6 elements where 8 are expected. My list also gets converted to an std::vector<unsigned char> when I call the function.Thing
@Burtonium: uint8_t is a typedef for an unsigned 8 bit type. That type is usually, if not always, unsigned char. So it's not converting it from std::vector<std::uint8_t> to std::vector<unsigned char>. The two types are the same, no conversion required.Obligation
O
18

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);
Obligation answered 15/12, 2015 at 22:25 Comment(0)
G
12

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.

Guideline answered 15/12, 2015 at 22:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.