delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/11/30/16:45:09

From: dalitz AT ulm DOT netsurf DOT de (dalitz)
Newsgroups: comp.os.msdos.djgpp
Subject: Strange behavior of fread
Date: Mon, 30 Nov 1998 21:25:21 GMT
Organization: eXtension GmbH, Germany
Lines: 52
Message-ID: <36630cd2.17414449@news.extension.net>
NNTP-Posting-Host: news.extension.net
X-Newsreader: Forte Free Agent 1.11/32.235
Cache-Post-Path: ns1.xtension.net!unknown AT dyn1 DOT ulm DOT netsurf DOT de
X-Cache: nntpcache 2.3.2 (see http://www.nntpcache.org/)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019