*BSD News Article 66066


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.anu.edu.au!harbinger.cc.monash.edu.au!news.rmit.EDU.AU!news.unimelb.EDU.AU!munnari.OZ.AU!spool.mu.edu!howland.reston.ans.net!newsfeed.internetmci.com!in2.uu.net!news.micron.net!dwight
From: dwight@micron.net (Dwight Tovey)
Newsgroups: comp.unix.bsd.misc,alt.unix.wizards,comp.unix.misc
Subject: Re: How to write end of file character into file??!
Date: 18 Apr 1996 15:00:17 GMT
Organization: Wish I had some
Lines: 32
Message-ID: <4l5le1$amd@is05.micron.net>
References: <xsvarshney-0604962038290001@newshub.csu.net> <4kb6sv$399@is05.micron.net> <4khh63$a3@anorak.coverform.lan> <4ktvmc$5d6@is05.micron.net> <4kuonh$q2@anorak.coverform.lan>
Reply-To: dwight@micron.net
NNTP-Posting-Host: dwight.boi.micron.net
X-newsreader: xrn 8.01
Xref: euryale.cc.adfa.oz.au comp.unix.bsd.misc:744 alt.unix.wizards:3654 comp.unix.misc:22040

In article <4kuonh$q2@anorak.coverform.lan>, brian@awfulhak.demon.co.uk (Brian Somers) writes:
|> Of course you're correct !  But retrospectively, I now believe that the
|> original poster was getting at this sort of code:
|> 
|>     int ch;
|>     if( fp = fopen( ... ), !fp )
|> 	...
|>     while( ch = fgetc( fp ), ch != EOF )
|> 	...
|>     fclose( fp );
|> 
|> The mistake that you can make here is thinking that EOF is a character.
|> EOF has however a value outside a 'char' range, and is thus a status.
|> 
|> --
|> Brian <brian@awfulhak.demon.co.uk>
|> Don't _EVER_ lose your sense of humour....

Right!  That's why I brought up the feof thing.  In a text file, looking for
ch != EOF will work since the only time you should get back EOF on a text file
is when you've read the last byte of data.  For a binary file, the EOF value can
be a valid result from fgetc (or whatever read), so the test for ch != EOF can
cause your program to think it is finished too early.  It's better to always
check for the feof STATUS instead of the EOF CHARACTER.
	/dwight
-- 
Dwight N. Tovey              H&W Computer Systems, Inc.
Software Specialist III      12438 W. Bridger St.  Suite 100
   dwight@hwcs.com           Boise, ID.  83713
or dwight@micron.net         (208)377-0336
I didn't claw my way to the top of the food chain to eat vegetables!!