home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!portal!dfuller
- From: dfuller@portal.hq.videocart.com (Dave Fuller)
- Subject: Re: Need help reading floats from file
- Message-ID: <BxBxvA.6Gp@portal.hq.videocart.com>
- Organization: VideOcart Inc.
- X-Newsreader: Tin 1.1 PL3
- References: <cee1.721108743@Ra.MsState.Edu>
- Date: Sat, 7 Nov 1992 04:59:33 GMT
- Lines: 57
-
- cee1@Ra.MsState.Edu (Charles Evans) writes:
- : 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..
- :
- : data file looks like:
- :
- : .11 .33 .00 .......
- : .23 .45 .23 .......
- : . . .
- : . . .
- :
- : 20 x 20
- :
- Chuck,
- as long as its definitely a 20x20 this will work. if the size is variable
- or if it may change then this needs to change.
-
-
- float numbers[20][20];
-
- /* note that each of the first elements has associated with it the
- /* 20 numbers that were on the line in the file
- /*
- /* i.e. numbers[1][5] = 6th number in the first line
- /* yes 6th, 0 indexing here . . .
-
- FILE *fp;
- int i,
- j;
-
- fp = fopen("file", "r");
-
- for(i = 0; i < 20; i++)
- {
- for(j = 0; j < 20; j++)
- {
- fscanf(fp, "%f", &(numbers[i][j]));
- }
- }
-
- fclose(fp);
-
- you don't need fgets because fscanf will continue past the end of
- a line, it only fails at EOF.
-
- Hope this helps you
-
- Dave Fuller
- dfuller@portal.hq.videocart.com
-
-
-