Strange Problem with Streams (wrong bytes read)

Hi,

I have a very strange problem:
When I try to read data from a file and print it byte by byte, I get some wrong
values and some correct. For example the first two bytes are correct, the next
few bytes are incorrect (there are also some completely impossible values, for
example fffffff5 which is >ff), then the next 6 bytes are correct again...
To me it looks like there are multiple bytes put together for some reason ???
But strangely the amount of bytes read is equal to the filesize.
I did this with the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char* argv[]) { 
char* filestr = "imgplane.b3d";

ifstream file (filestr, ios::in|ios::binary|ios::ate);//create stream
  if (file.is_open())
  {
	int size = file.tellg();
	cout <<"Size: "<<size<<endl;
	char* memblock = new char [size];
	file.seekg (0, ios::beg);
	file.read (memblock, size);//copy file into memory
	file.close();

	cout << std::hex;
	for(int i=0; i<size; i++){//for each byte...
		if(i>0){cout << ", ";}
		int out = memblock[i];
		cout <<out;//...print the byte in hexadecimal format
	}


	delete[] memblock;
}


return 0;
}




return 0;
}



Output (hexadecimal format):

Size: 253
42, 42, 33, 44, fffffff5, 0, 0, 0, 1, 0, 0, 0, 54, 45, 58, 53, 0, 0, 0, 0, 42, 52, 55, 53, 4, 0, 0, 0, 1, 0, 0, 0, 4e, 4f, 44, 45, ffffffd5, 0, 0, 0, 52, 4f, 4f, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ffffff80, 0, 0, ffffff80, 3f, 0, 0, ffffff80, 3f, 0, 0, ffffff80, 3f, 0, 0, ffffff80, 3f, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ffffff80, 4d, 45, 53, 48, ffffff8c, 0, 0, 0, ffffffff, ffffffff, ffffffff, ffffffff, 56, 52, 54, 53, 5c, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ffffff80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ffffff80, ffffffbf, 0, 0, 0, ffffff80, 0, 0, 0, 0, 0, 0, ffffff80, 3f, 0, 0, ffffff80, 3f, 0, 0, ffffff80, ffffffbf, 0, 0, 0, ffffff80, 0, 0, ffffff80, 3f, 0, 0, ffffff80, 3f, 0, 0, ffffff80, 3f, 0, 0, 0, 0, 0, 0, 0, ffffff80, 0, 0, ffffff80, 3f, 0, 0, 0, 0, 54, 52, 49, 53, 1c, 0, 0, 0, ffffffff, ffffffff, ffffffff, ffffffff, 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 41, 4e, 49, 4d, c, 0, 0, 0, 0, 0, 0, 0, ffffffc8, 0, 0, 0, 0, 0, 0, 0


hexdump of the file:

0000000 4242 4433 00f5 0000 0001 0000 4554 5358
0000010 0000 0000 5242 5355 0004 0000 0001 0000
0000020 4f4e 4544 00d5 0000 4f52 544f 0000 0000
0000030 0000 0000 0000 0000 0080 8000 003f 8000
0000040 003f 8000 003f 8000 003f 0000 0000 0000
0000050 0000 0000 4d80 5345 8c48 0000 ff00 ffff
0000060 56ff 5452 5c53 0000 0000 0000 0100 0000
0000070 0200 0000 0000 0000 0000 0000 0000 0000
0000080 0080 0000 0000 0000 0000 0000 0000 8000
0000090 00bf 0000 0080 0000 0000 8000 003f 8000
00000a0 003f 8000 00bf 0000 0080 8000 003f 8000
00000b0 003f 8000 003f 0000 0000 0000 0080 8000
00000c0 003f 0000 5400 4952 1c53 0000 ff00 ffff
00000d0 00ff 0000 0200 0000 0100 0000 0000 0000
00000e0 0300 0000 0200 0000 4100 494e 0c4d 0000
00000f0 0000 0000 c800 0000 0000 0000 0000 





Thank you very much in advance for your help.
Last edited on
Change line 21 to : unsigned out = static_cast<unsigned char>(memblock[i]) ;
Last edited on
You don't say what machine platform you running on, but what you are doing is NOT portable. You're getting the bytes into memory LSB then MSB.

4242 prints out correrctly because the LSB and MSB are the same.
4433 prints out as 3344 again because the bytes are reversed.
With 00f5, the f5 is stored in a signed char so when moved to a signed integer, the sign is extended becoming fffffff5, then you output the 0.
0000 appears correct because the bytes are the same.
But with 0001, the bytes are reversed: 1, 0
4554 becomes 54, 45
etc, etc, etc.

@cire - That will fix the fffffff5, but won't fix the order of the bytes.
Last edited on
The code is portable, the output of the hexdump depends on the endianness of the OS.

If Dolfbutt was reading shorts (2 bytes at a time), there would be issues.
@cire - That will fix the fffffff5, but won't fix the order of the bytes.


You must know something I don't about the format of the file. The "hex dump" is obviously interpreting the file as 2-byte words. But.. it's a hex dump. You want a view of the bytes, set it up to show the dump as individual bytes.
Thank you very much for your replies.
It works now :-)

The solution posted by cire worked.
The bytes were only in the wrong order in the hexdump output.
hexdump -C imgplane.b3d printed the right order.
@cire - You're right. I assumed the hex dump was showing the true order of the bytes and jumped to the conclusion that OP was getting the wrong byte order.
Topic archived. No new replies allowed.