home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16137 < prev    next >
Encoding:
Text File  |  1992-11-08  |  1.6 KB  |  69 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!portal!dfuller
  3. From: dfuller@portal.hq.videocart.com (Dave Fuller)
  4. Subject: Re: Need help reading floats from file
  5. Message-ID: <BxBxvA.6Gp@portal.hq.videocart.com>
  6. Organization: VideOcart Inc.
  7. X-Newsreader: Tin 1.1 PL3
  8. References: <cee1.721108743@Ra.MsState.Edu>
  9. Date: Sat, 7 Nov 1992 04:59:33 GMT
  10. Lines: 57
  11.  
  12. cee1@Ra.MsState.Edu (Charles Evans) writes:
  13. : Ok, in getting data for an experiment to look at, I have
  14. : 200 .xx floating point number, just a decimal point and 2 decimal
  15. : places, put in 20 x 20 in a data file.
  16. : How can I read them into an array.. all i could think of was do 
  17. : a HUGE fgets/sscanf thing with "%f %f ... %f".. 20 times and put into
  18. : 20 parts of an array like : value[i++][j] on an on until i=20 then
  19. : reset i and increment j..
  20. : data file looks like:
  21. : .11 .33 .00 .......
  22. : .23 .45 .23 .......
  23. :  .   .   .
  24. :  .   .   .
  25. : 20 x 20
  26. Chuck,
  27.   as long as its definitely a 20x20 this will work. if the size is variable
  28. or if it may change then this needs to change.
  29.  
  30.  
  31. float numbers[20][20];
  32.  
  33. /* note that each of the first elements has associated with it the 
  34. /* 20 numbers that were on the line in the file
  35. /*
  36. /*  i.e.  numbers[1][5] = 6th number in the first line
  37. /*                        yes 6th, 0 indexing here . . . 
  38.  
  39. FILE *fp;
  40. int  i,  
  41.      j;
  42.  
  43. fp = fopen("file", "r");
  44.  
  45. for(i = 0; i < 20; i++)
  46. {
  47.    for(j = 0; j < 20; j++)
  48.    {
  49.       fscanf(fp, "%f", &(numbers[i][j]));
  50.    }
  51. }
  52.  
  53. fclose(fp);
  54.  
  55. you don't need fgets because fscanf will continue past the end of
  56. a line, it only fails at EOF.
  57.  
  58. Hope this helps you
  59.  
  60. Dave Fuller
  61. dfuller@portal.hq.videocart.com
  62.  
  63.  
  64.