home *** CD-ROM | disk | FTP | other *** search
- 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
- From: ta-psc10@cunixa.cc.columbia.edu (Po Shan Cheah)
- Newsgroups: comp.lang.c
- Subject: Re: fgets() sscanf() EOF confusion
- Message-ID: <1992Dec30.165902.14866@news.columbia.edu>
- Date: 30 Dec 92 16:59:02 GMT
- References: <1992Dec30.160832.3769@coe.montana.edu>
- Sender: usenet@news.columbia.edu (The Network News)
- Reply-To: ta-psc10@cunixa.cc.columbia.edu (Po Shan Cheah)
- Organization: Columbia University
- Lines: 32
- Nntp-Posting-Host: cunixa.cc.columbia.edu
-
- In article <1992Dec30.160832.3769@coe.montana.edu> sandy@cs.montana.edu (Sandy Pittendrigh) writes:
- >while(!feof(inFile))
- > {
- > fgets(line,80,inFile);
- > sscanf(line,"%d %d %d %d", &x, &y, &z);
- > if(eof(inFile) /* But this doesn't work */
- > {
- > doSomething(x,y,z);
- > break;
- > }
- > else doSomething(x,y,z);
- > }
- >
- >Is this a resonable concept?
-
- Try doing it this way:
-
- while (fgets(line,80,inFile) != NULL) {
- sscanf(line,"%d %d %d", &x, &y, &z);
- doSomething(x,y,z);
- }
-
- if (ferror(inFile)) {
- /* An error occurred. Do something about it. */
- }
-
- This is based on the fact that fgets() returns a NULL pointer if an
- EOF or a read error occurs.
- Po Shan Cheah
- ta-psc10@columbia.edu
- pc30@columbia.edu
- pcheah@nyx.cs.du.edu
-