From: "DeHackEd" Newsgroups: comp.os.msdos.djgpp References: <36630cd2 DOT 17414449 AT news DOT extension DOT net> Subject: Re: Strange behavior of fread Lines: 72 X-Newsreader: Microsoft Outlook Express 4.72.3110.5 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 Message-ID: Date: Mon, 30 Nov 1998 21:18:49 GMT NNTP-Posting-Host: 142.194.222.233 NNTP-Posting-Date: Mon, 30 Nov 1998 21:18:49 GMT To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Probably a processor processing the data differently. If you know this is happening, try reading in 1 byte at a type and << the appropriate byte 8 places. Or you could try something like this: inline short byteflip(short value) { return value >> 8 | ((value&0xff) << 8); } I doubt it would make a difference, but try some optimizations and -mpentium -- "DeHackEd" My Email address in the header is fake (spammers). Email me at this address: http://www.geocities.com/cgi-bin/homestead/mail.pl?dehacked dalitz wrote in message <36630cd2 DOT 17414449 AT news DOT extension DOT net>... >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. >