home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!europa.asd.contel.com!darwin.sura.net!zaphod.mps.ohio-state.edu!rpi!batcomputer!munnari.oz.au!metro!sequoia!acacia!mgream
- From: mgream@acacia (Matthew Gream)
- Newsgroups: comp.lang.c
- Subject: Re: Need help reading floats from file
- Date: 8 Nov 1992 22:09:21 GMT
- Organization: University of Technology, Sydney
- Lines: 52
- Message-ID: <1dk36hINNpq3@sequoia.ccsd.uts.EDU.AU>
- References: <cee1.721108743@Ra.MsState.Edu>
- NNTP-Posting-Host: acacia.ccsd.uts.edu.au
- X-Newsreader: Tin 1.1 PL5
-
- Charles Evans (cee1@Ra.MsState.Edu) wrote:
-
- : Ok, in getting data for an experiment to look at, I have
- : 200 .xx floating point number, just a decimal point and 2 decimal
- : places, put in 20 x 20 in a data file.
- :
- : How can I read them into an array.. all i could think of was do
- : a HUGE fgets/sscanf thing with "%f %f ... %f".. 20 times and put into
- : 20 parts of an array like : value[i++][j] on an on until i=20 then
- : reset i and increment j..
- :
- : but i get weird errors from dos and qemm.. something is wrong.
- : why cant i (i can in basic :)) just keep reading in a floating point number
- : or read it in as a small string until the first whitespace?
- :
- : or something. thanks
- :
- : data file looks like:
- :
- : .11 .33 .00 .......
- : .23 .45 .23 .......
- : . . .
- : . . .
- :
- : 20 x 20
-
- Try using strtok() and atof(), for example, your code could look
- something like:
-
- FILE *FILE_handle;
- char Buffer[MAXBUFLEN];
- int row, column;
- double big_array[MAX_ROWS][MAX_COLUMNS];
-
- row = 0;
- while( fgets(Buffer,sizeof(Buffer),FILE_handle) && (row < MAX_ROWS) ) {
- register char *char_ptr;
- if(! (char_ptr = strtok(Buffer," \t\r\n")) )
- continue;
- column = 0;
- while( char_ptr = strtok(NULL," \t\r\n") )
- big_array[row][column++] = atof(char_ptr);
- row++;
- }
-
- That may or may not be what you want, but it should give you some
- good ideas. The reason qemm is probably barfing is that your passing
- an incorrect pointer (or something to that effect) in sscanf.
-
- Matthew.
- --
- M.Gream@uts.EDU.AU - Matthew Gream - 'Edges have holes next to them .. '
-