i/o of floats
Richard A. O'Keefe
ok at goanna.cs.rmit.oz.au
Wed Aug 22 13:20:28 AEST 1990
In article <9232 at jpl-devvax.JPL.NASA.GOV>, charest at ai-ganymede.JPL.NASA.GOV (Len Charest) writes:
> I have a large (~40 MB) file of floating point values stored in binary
> [using IEEE-754 single precision (32-bit) format]
> 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.
Ick. You can't use fscanf() with such data *at all*. fscanf() is for
reading *text*. To read a record of N bytes as N/4 floats, do
#define N /*whatever*/
#define NFLOATS ((N)/sizeof (float))
float the_record[NFLOATS];
int floats_read;
...
floats_read = fread(the_record, sizeof (float), NFLOATS, the_stream);
Now you have 32-bit floats in memory and can do
printf("%g", the_record[floats_read-1])
or whatever takes your fancy.
--
The taxonomy of Pleistocene equids is in a state of confusion.
More information about the Comp.lang.c
mailing list