Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uunet!convex!convex.COM From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.lang.c Subject: Re: i/o of floats Message-ID: <105212@convex.convex.com> Date: 22 Aug 90 10:56:26 GMT References: <9232@jpl-devvax.JPL.NASA.GOV> Sender: news@convex.com Reply-To: tchrist@convex.COM (Tom Christiansen) Organization: CONVEX Software Development, Richardson, TX Lines: 40 In article <9232@jpl-devvax.JPL.NASA.GOV> charest@AI-Cyclops.JPL.NASA.GOV writes: >I have a large (~40 MB) file of floating point values stored in binary >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. If I understand your problem, it's pretty easy to solve. They're floats on the disk, so read them in that way. I assume that your machine is also in IEEE format. Otherwise you'll have to write an IEEE- to-native conversion function, probably using bit fields or bit masks. The simple case (same FP format) is easy. I use stdio because you did and it optimizes the I/O buffering pretty well. /* * read n floats in binary from stdio pointer fp, * printing on stdout one per line. returns i/o success. * CAVEAT EMPTOR: untested */ #define FOK(F) (!(feof(F) || ferror(F))) read_floats(fp, n) FILE *fp; int n; { float num; ASSERT(fp != NULL); ASSERT(n > 0); while (FOK(fp) && n-- && fread(&num, sizeof(num), 1, fp)) printf("%14.8f\n", num); return FOK(fp); } --tom -- "UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." [Doug Gwyn]