*BSD News Article 66799


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.anu.edu.au!harbinger.cc.monash.edu.au!news.cs.su.oz.au!inferno.mpx.com.au!news.unimelb.EDU.AU!munnari.OZ.AU!spool.mu.edu!daily-planet.execpc.com!homer.alpha.net!uwm.edu!cs.utexas.edu!howland.reston.ans.net!newsxfer.itd.umich.edu!news.mathworks.com!fu-berlin.de!zib-berlin.de!uni-duisburg.de!hal6000!michael
From: michael@hal6000.thp.Uni-Duisburg.DE (Michael Staats)
Newsgroups: comp.unix.bsd.misc,alt.unix.wizards,comp.unix.misc
Subject: Re: How to write end of file character into file??!
Date: 23 Apr 1996 08:06:08 GMT
Organization: Theoretische Physik, Uni-GH-Duisburg, Germany
Lines: 41
Distribution: world
Message-ID: <4li31g$qsm@h20-hrze.uni-duisburg.de>
References: <xsvarshney-0604962038290001@newshub.csu.net> <4ktvmc$5d6@is05.micron.net> <4kuonh$q2@anorak.coverform.lan> <4l5le1$amd@is05.micron.net> <4l85m7$fjs@sv024.SanDiegoCA.ATTGIS.COM> <4lg9i8$8if@is05.micron.net>
NNTP-Posting-Host: hal6000.uni-duisburg.de
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Xref: euryale.cc.adfa.oz.au comp.unix.bsd.misc:828 alt.unix.wizards:3711 comp.unix.misc:22188

dwight@micron.net (Dwight Tovey) writes:


>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.

As already said, this code is wrong.
And your definition of text-file seems to be "file of
characters below 0x80". But 0xff is a valid character.

BUT even with pure 7 bit ASCII input your code will fail on machines with
unsigned characters as default, e. g. IBM RS/6k under AIX. Unsigned
chars are never < 0, so your program will nerver stop on such
machines. (Note that it is implementation specific wether chars are
signed or unsigned as default.)

It is plain wrong to read the input into a "char" variable.

AND if you do it correct (read into int) the method of testing against
EOF is as good as using feof(). feof() is not "better".

Bye,
	Michael
--
Michael Staats, Theoretical Physics, Uni-GH Duisburg 
email: michael@thp.Uni-Duisburg.DE
<a href="http://WWW.thp.Uni-Duisburg.DE/">Click</a> me!
<a href="http://WWW.thp.Uni-Duisburg.DE/cuaix/cuaix.html">A c.u.aix archive</a>