delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/07/29/14:09:04

Date: Fri, 29 Jul 1994 09:49:28 -0400 (EDT)
From: Edwin Phillips <flaregun AT strauss DOT udel DOT edu>
Subject: Re: help with reading floats from file
To: Leendert Combee <combee AT cambridge DOT scr DOT slb DOT com>
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu

On Fri, 29 Jul 1994, Leendert Combee wrote:

> 
>   Hello, 
> 
>   I have trouble reading binary floating point data (4bytes/value) from a
>   file generated on a sun/unix into my code compiled with djgpp.
> 
>   If I compile and run my code on my sun as well, I have no problem: the
>   code I use is
> 
>   float data[...] ;
>   File *file      ;
> 
>   ....
> 
>   fread(data,sizeof(float),ns,file) ;
> 
>   It doesn't seem to matter whether I open the file as "r" or "rb", whether
>   I compile with cc or gcc, it all works. If I now compile the same source 
>   with djgpp on my pc, the values that are read from the same file are not
>   read as proper floats (sizeof(float) is still 4bytes). Do I need to swap
>   some bytes, has anyone encountered this and knows what the proper format 
>   conversion is between floats in djgpp and floats under unix?
> 
>   Many thanks, Leendert.
> 
> 

	The order of bytes is different for a Sun than Intel x86.  Also, 
I don't even know if the floating point format is the same.  However, you 
can try to reorder the bytes after you read them in.  Here's something to 
try after they're read in (just reverse the order of each 32bit word):

	unsigned char *p;
	unsigned char *q;
	unsigned char ch;
	int i;

	...

	for (i=0; i<ARRAY_SIZE; i++)
	{
		p = (char *) &data[i];
		q = p + 3;
		ch = *q;
		*q = *p;
		*p = ch;
		p++;
		q--;
		ch = *q;
		*q = *p;
		*p = ch;
	}

	Give it a whirl!  I have know idea if it'll work. :(

	A sure way to do it (although not efficient) is to use printf() to 
write the numbers out, and scanf() to read them in.  That's the only 
_sure_ way to avoid this kind of incompatibility. ;-)

	Ed

/****************************************************************************/
/* Ed Phillips  flaregun AT strauss DOT udel DOT edu      University of Delaware       */
/* Jr Systems Programmer  (302) 831-6082    IT/Network and Systems Services */
/****************************************************************************/


- Raw text -


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