i/o of floats

Len Charest charest at ai-ganymede.JPL.NASA.GOV
Wed Aug 22 08:43:10 AEST 1990


I have a large (~40 MB) file of floating point values stored in binary
under the following format:
	float = 32-bit word
	bit 31 = sign bit for exponent
	bits 30-23 = exponent (8 bits)
	bits 22-0 = mantissa (always positive)
(Apparently this is some IEEE standard.)
The floats are stored in contiguous "records" of length N, where N is
determined at *run-time*. This means that I can minimize the number of
calls to fscanf by reading an entire record of floats from the disk (as
an array of chars) and then parsing the record in C. Example:
	let N = 256			/* N is known a priori for now */
	unsigned char record[N + 1];	/* add one for terminating `\0` */
	fscanf(fp, "%256s", record); 	/* read the record from disk */
Now how do I interpret each 4-byte "word" of record as a floating point number?

I merely need to print each float onto stdout. However, I've been away
from C programming for a while and I'm confused about how to solve this
problem. I am assuming that the fscanf library function does not alter
the bits it reads when the conversion spec indicates a character string.
That is, interpretation of the raw bits stored on file should be
completely under my control. On the other hand, I really would like to
avoid writing a mammoth floating point conversion routine.

Suggestions/solutions are greatly appreciated. Respond to this list or
directly to me at charest at ai-cyclops.jpl.nasa.gov

Thanx,
Len



More information about the Comp.lang.c mailing list