home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 19152 < prev    next >
Encoding:
Text File  |  1993-01-03  |  2.6 KB  |  72 lines

  1. Path: sparky!uunet!ogicse!usenet.coe.montana.edu!rpi!gatech!usenet.ins.cwru.edu!po.CWRU.Edu!rat
  2. From: rat@po.CWRU.Edu (Reza A. Tabib-Azar)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: ** HELP WITH FILE I/O **
  5. Message-ID: <1i7boaINNbku@usenet.INS.CWRU.Edu>
  6. Date: 3 Jan 93 18:36:58 GMT
  7. Article-I.D.: usenet.1i7boaINNbku
  8. References: <JmeswB1w165w@tradent.wimsey.bc.ca>
  9. Reply-To: rat@po.CWRU.Edu (Reza A. Tabib-Azar)
  10. Organization: Case Western Reserve University, Cleveland, OH (USA)
  11. Lines: 58
  12. NNTP-Posting-Host: slc12.ins.cwru.edu
  13.  
  14.  
  15. In a previous article, lord@tradent.wimsey.bc.ca (Jason Cooper) says:
  16.  
  17. >Eeek!  I am writing a program to handle a local track meet and have to 
  18. >input the participant records from a file.  What I want to do is load 
  19. >these into an array for quick and easy access.  The array is right now of 
  20. >undefined length, and its elements are structures (including each event, 
  21. >it's heat, it's event, etc.).  I've tried using:
  22. >
  23. >fread(meet[record], sizeof(struct meetstruct), 1, tfile)
  24. >
  25. >Where meet is the array, meetstruct is the structure the array is 
  26. >composed of, and tfile is the track meet's file stream.  I don't know 
  27. >_what_ I'm doing wrong, but if somebody could correct this (oops, forgot 
  28. >to mention, that's in a loop to go through records, I tried taking the 
  29. >whole thing in one, but that didn't work either), then I'd appreciate 
  30. >that VERY MUCH.  If I could input the whole thing in one with fread or 
  31. >something, that would be _WONDERFUL_.  I'm using Borland C++, and would 
  32. >like to understand _what_ I'm doing, so if you could explain it too, that 
  33. >would be nice.  If I have to limit the size of the array, that's doable 
  34. >too.
  35.  
  36.  
  37.         [Stuff deleted]
  38.  
  39. You see, fread and frwite are used to read and write a block of data. For
  40.  
  41. example, you can write your whole array to a file and read it in one call as
  42. well. Therefore, you do not need to put the fread call in a loop. The first
  43. argument of fread is pointer to an array. Here is an example to for you
  44. to see, it will write a block of data and the data will be read back in.
  45.  
  46. typedef struct {
  47.     int a;
  48.     char b;
  49. } foo;
  50.  
  51. main()
  52. {
  53.         FILE *fp;
  54.         foo inbuff[3];
  55.         foo outbuff[3] = { {1, 'q'}, { 2, 'w'}, { 3, 'e'}};
  56.  
  57.         fp = fopen("myfoofile", "wb+");
  58.         fwrite( outbuff, sizeof(foo), 3, fp);
  59.         rewind( fp );
  60.         fread( inbuff, sizeof(foo), 3, fp);
  61.         /* do what ever you like to do with the inbuff array (no magic) */
  62.         fclose(fp);
  63. }
  64.  
  65. I hope this will clarify things a bit.
  66.  
  67.  
  68. -- 
  69. main() { printf("\t\tReza A. Tabib-Azar\b\n"); 
  70.        printf("Life is real, unless declared an integer.\n");
  71.   printf("I get 'CoreDumped', therefore I am.\n", "\t\t\t\tAnonymous\n"); }
  72.