This is my first time posting so bear with me.
I have a file that I need to read the header in via redirection and I’m using cin to capture the bytes and store them for later use.
The hexdump for the file is:
42 02 02 02 ff 0a 00 00 00 19 00 00 00 ff
What I have:
char magic, cs1, cs2, cs3, selector;
char temp1, temp2, temp3, temp4;
cin >> magic;
cin >> cs1;
cin >> cs2;
cin >> cs3;
cin >> selector;
//
cin >> temp1;
cin >> temp2;
cin >> temp3;
cin >> temp4;
The first 5 bytes represent magicNumber, channelSize, channelSize, channelSize, selectorBit.
The next 4 bytes are a width value separated into 4 bytes in LSB to MSB.
When I use cin on 0a which is 10 in hex (the value I want) it reads it as a newline character and thus skips it.
cout << temp1 << endl;
cout << temp2 << endl;
cout << temp3 << endl;
cout << temp4 << endl;
This will contain (in hex):
0
0
0
19
cin skips over 0a and grabs the next value. Is there a way to force it to read 0a?
If any more info is needed, let me know. Thanks
— Kyle
This is because
cin, which is anistreamexpects the data to be text.If you are working with binary data then
istream::getandistream::readare what you are looking for.Retrieves four byte values from the input stream.
Will read the block of data into an array of chars for you. You could use a plain integer
if the endianness matches that of the data.