*BSD News Article 66757


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!inferno.mpx.com.au!news.mel.aone.net.au!imci4!newsfeed.internetmci.com!in1.uu.net!ncrgw2.ncr.com!ncrhub2!rap.SanDiegoCA.ATTGIS.COM!not-for-mail
From: mk@sparc.SanDiegoCA.ATTGIS.COM (Mark Kaufman)
Newsgroups: comp.unix.bsd.misc,alt.unix.wizards,comp.unix.misc
Subject: Re: How to write end of file character into file??!
Date: 22 Apr 1996 14:17:25 -0700
Organization: AT&T Global Information Solutions, San Diego, CA
Lines: 97
Message-ID: <4lgt15$223@sv024.sandiegoca.attgis.com>
References: <xsvarshney-0604962038290001@newshub.csu.net> <4l5le1$amd@is05.micron.net> <4l85m7$fjs@sv024.SanDiegoCA.ATTGIS.COM> <4lg9i8$8if@is05.micron.net>
Reply-To: Mark.Kaufman@SanDiegoCA.ATTGIS.COM
NNTP-Posting-Host: sv024.sandiegoca.attgis.com
Xref: euryale.cc.adfa.oz.au comp.unix.bsd.misc:818 alt.unix.wizards:3706 comp.unix.misc:22179

In article <4lg9i8$8if@is05.micron.net>,
Dwight Tovey <dwight@micron.net> wrote:
>In article <4l85m7$fjs@sv024.SanDiegoCA.ATTGIS.COM>, mk@sparc.SanDiegoCA.ATTGIS.COM (Mark Kaufman) writes:
>|> In article <4l5le1$amd@is05.micron.net>,
>|> Dwight Tovey <dwight@micron.net> wrote:
>|> >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.
>|> >|> 
>|> >
>|> >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.
>|> 
>|> I am sure other flames are already on their way, so I'll be brief:
>|> You have no clue.
>|> 
>|> When it succeeds, fgetc always returns a *character* value in an *int*.
>|> "character" means one byte, 0x00 through 0xFF, integer value 0 through
>|> +255.  On EOF, fgetc returns the integer value (-1).  If you are reading
>|> a character at a time then there is NO difference between a "text" file
>|> and a "binary" file.  The poster to whom you were replying was exactly
>|> correct - you misinterpreted him in support of your flawed understanding.
>|> 
>|> I don't know how I can be any clearer, so if you are still confused please
>|> go read the man page for fgetc real carefully.
>|> 
>
>Are you always this abrasive to people or do you reserve it for Usenet.  Nice
>attitude for a "Manager".

In fact, I felt I showed a bit of restraint considering I was responding to
this in alt.unix.wizards...  And you can ask the members of my group if they
feel I am abrasive, but then again none of them would make this mistake.

>
>At any rate, what I was trying to point out (possibly not very well) is that
>testing for the EOF "value" can cause problems.  I have seen code similar to the
>following:
>
>	char ch;
>	FILE *fp
>
>	fp = fopen( "foo", "r" );
>	while(( ch = fgetc( fp) ) > 0 ) {
>		putchar( ch );
>	}
>
>This will not work on a binary file but it will work on a text only file.  Go
>ahead and try it.  Put a 0xFF into the middle of an otherwise text file and read
>it with this code.  It will stop when it hits the 0xFF.  However the following
>code will go to the "real" end of the file for both binary and text files:
>
>	char ch;
>	FILE *fp
>
>	fp = fopen( "foo", "r" );
>	while( !feof( fp )) {
>		ch = fgetc(fp);
>		putchar( ch );
>	}
>
>
>Once again, I don't claim that the code I am showing here is the "best" way to do
>anything.  My only intention is to point out that testing for the "EOF character"
>on a read can lead to confusion when you get a file that has non-text data.

With the above clarifications I will grant your point.  However, you were
responding to a post that (correctly) declared 'int ch;', so my remarks
were warranted.

>
>FWIW: I have worked in Unix for more than a few years, including porting Unix
>kernel & library routines to different platforms.  I still don't claim to be an
>"expert" at anything, but I do have a passing familiarity with some of it.

Then we are on approximately equal footing :-).  If you want to continue
this discussion or comment further on my attitude, please feel free to
email me so the newgroups don't have to read any more.  Thanks.

-- 
Mark J. Kaufman, Manager, UNIX/PDE Projects
mark.kaufman@sandiegoca.attgis.com
(619) 485-3891   VoicePlus 440