Mail Archives: djgpp/1998/11/30/16:45:09
Hello folks,
I observed an unexpected behavior of fread.
It seems to me that the order of two subsequent bytes
is swapped in the buffer when the fread function reads items of
two bytes (short int). I really don't understand what's going
on.
Let me give you an example:
...
long int offset;
short int *value;
...
ifile = fopen("binary.dat","rb");
ofile = fopen("text.dat","w");
...
offset = 8*320;
for (i=0; i<=160; i++){
fseek (ifile, offset+2*i, SEEK_SET);
fread (value, sizeof(short int), 1, ifile);
fprintf(ofile,"Offset:%d\t%d\n",offset+2*i,*value);}
...
The hex dump of binary.dat looks like that:
Offset Data
... ...
000a90 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
000aa0 00 02 00 04 00 03 04 88 35 63 00 02 00 00 00 00
000ab0 00 01 00 C8 00 8C 00 01 00 13 00 04 00 0A 00 1C
... ...
This gives the following output in text.dat:
...
Offset:2720 512 this is 02 00 instead of 00 02
Offset:2722 1024 this is 04 00 instead of 00 04
Offset:2724 768 this is 03 00 instead of 00 03
Offset:2726 -30716 this is not 04 88 but may be the result of
88 04
Offset:2728 25397 this is 63 35 instead of 35 36
...
I tried that with VC++ and djgpp on both Win NT and Win 95. The
results were all the same.
binary.dat was downloaded from an HP-UX 9.0x server an was created
by some Fortran Code.
Does anybody know what's going on here?
I would appreciate any comment or idea. Thank you in advance.
Dalitz.
- Raw text -