home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 16646 < prev    next >
Encoding:
Internet Message Format  |  1992-11-17  |  1.7 KB

  1. Path: sparky!uunet!charon.amdahl.com!pacbell.com!decwrl!sun-barr!cs.utexas.edu!zaphod.mps.ohio-state.edu!rpi!batcomputer!munnari.oz.au!goanna!ok
  2. From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: problem with foef()
  5. Message-ID: <15987@goanna.cs.rmit.oz.au>
  6. Date: 17 Nov 92 08:40:30 GMT
  7. References: <92316.213238U17868@uicvm.uic.edu>
  8. Organization: Comp Sci, RMIT, Melbourne, Australia
  9. Lines: 40
  10.  
  11. In article <92316.213238U17868@uicvm.uic.edu>, U17868@uicvm.uic.edu writes:
  12. > I am reading a binary file with fread.
  13.  
  14. Then you don't need feof().  The basic paradigm for C input is
  15.  
  16.     while (try_to_read(...) SUCCEEDS) {
  17.         use_the_input(...);
  18.     }
  19.     the loop stopped because of EOF or because of error,
  20.     use feof() to tell which.
  21.  
  22. >   #include "header.h"
  23.  
  24. >   void dispdail(date)
  25. >       char date[9];
  26.  
  27. WHOOPS!  This does not mean what you think it does!  A simple rule of
  28. thumb which will help you get C functions right is
  29.     *NEVER* use [] in the arguments of a function.
  30. Look under "argument promotion rules" in the index of any good C text.
  31.  
  32. Use
  33.     void dispdail(date)
  34.         char *date;        /* or char date[]; if you _must_ */
  35. >       {
  36. >           int i, j, a;
  37. >           FILE *activity_file;    /* was 'file_pointer' which is */
  38.                     /* a very unhelpful name */
  39. >           struct activity temp;
  40.  
  41. >           activity_file = fopen("activity.dat", "rb");
  42.             assert(activity_file != NULL);
  43.         /* There is no need to use fseek(), "rb" starts at the beginning */
  44.             while (frad(&temp, sizeof temp, 1, activity_file) == 1) {
  45.             ... use this activity record ...
  46.             }
  47.             assert(feof(activity_file));
  48.             fclose(activity_file);
  49. >   }   /* end dispdail */
  50.  
  51.