I have a binary data file. There is a binary header of a few thousand bytes. I task is to read bytes 3000 and 3001. The good news is that it should read one of two values: 1 or 5. The hard part for me is that the number is stored as a two's complement integer. I will put in binary mode, but I do not where to go from there. I see one route since there are only two values but not sure how to go about it. I believe I need either 1 = 0000 0000 0000 0001 or 5 = 0000 0000 0000 0101, so I only need to look at byte 3001, the thing is I do not really know how to display a value from an individual byte. I need either the value of byte 3001, or if there is a way to access individual bits, it would be great to just display byte 3001 on the screen as either 0000 0001 or 0000 0101.
Asked
Active
Viewed 315 times
1 Answers
0
For your task " read bytes 3000 and 3001", just use simple seekg
cin.seekg(3);
char c;
cin >> c;
bitset < 8 > s(c);
cout << s;
With this code and input "abcdefg" you will get output "d";
But be careful when work with binary data. For example if you realy sure that from 3000 stored int(typical 4 byte) then there is no problem. But if on this place stored for example short, and you read into int, there it will be 100% some type of garbage and very high chance to corrupt next data.

POTEMKINDX
- 395
- 2
- 9
-
cin.seekg(0) would have yielded g right, seekg(1) would have yielded f? (with all the other code the same) – devin Salemi Oct 14 '15 at 08:00