home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 19034 < prev    next >
Encoding:
Internet Message Format  |  1992-12-30  |  1.3 KB

  1. Path: sparky!uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!uwm.edu!psuvax1!rutgers!news.columbia.edu!cunixa.cc.columbia.edu!ta-psc10
  2. From: ta-psc10@cunixa.cc.columbia.edu (Po Shan Cheah)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: fgets() sscanf() EOF confusion
  5. Message-ID: <1992Dec30.165902.14866@news.columbia.edu>
  6. Date: 30 Dec 92 16:59:02 GMT
  7. References: <1992Dec30.160832.3769@coe.montana.edu>
  8. Sender: usenet@news.columbia.edu (The Network News)
  9. Reply-To: ta-psc10@cunixa.cc.columbia.edu (Po Shan Cheah)
  10. Organization: Columbia University
  11. Lines: 32
  12. Nntp-Posting-Host: cunixa.cc.columbia.edu
  13.  
  14. In article <1992Dec30.160832.3769@coe.montana.edu> sandy@cs.montana.edu (Sandy Pittendrigh) writes:
  15. >while(!feof(inFile))
  16. >  {
  17. >  fgets(line,80,inFile);
  18. >  sscanf(line,"%d %d %d %d", &x, &y, &z);
  19. >  if(eof(inFile) /* But this doesn't work */
  20. >   {
  21. >   doSomething(x,y,z);
  22. >   break;
  23. >   }
  24. >  else doSomething(x,y,z);
  25. >  }
  26. >
  27. >Is this a resonable concept? 
  28.  
  29. Try doing it this way:
  30.  
  31. while (fgets(line,80,inFile) != NULL) {
  32.   sscanf(line,"%d %d %d", &x, &y, &z);
  33.   doSomething(x,y,z);
  34. }
  35.  
  36. if (ferror(inFile)) {
  37.  /* An error occurred. Do something about it. */
  38. }
  39.  
  40. This is based on the fact that fgets() returns a NULL pointer if an
  41. EOF or a read error occurs.
  42. Po Shan Cheah
  43. ta-psc10@columbia.edu
  44. pc30@columbia.edu
  45. pcheah@nyx.cs.du.edu
  46.