Return to BSD News archive
Path: euryale.cc.adfa.oz.au!newshost.anu.edu.au!harbinger.cc.monash.edu.au!news.bhp.com.au!mel.dit.csiro.au!munnari.OZ.AU!news.ecn.uoknor.edu!paladin.american.edu!gatech!newsfeed.internetmci.com!in2.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: 19 Apr 1996 06:49:59 -0700 Organization: AT&T Global Information Solutions, San Diego, CA Lines: 41 Message-ID: <4l85m7$fjs@sv024.SanDiegoCA.ATTGIS.COM> References: <xsvarshney-0604962038290001@newshub.csu.net> <4ktvmc$5d6@is05.micron.net> <4kuonh$q2@anorak.coverform.lan> <4l5le1$amd@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:780 alt.unix.wizards:3680 comp.unix.misc:22113 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. -- Mark J. Kaufman, Manager, UNIX/PDE Projects mark.kaufman@sandiegoca.attgis.com (619) 485-3891 VoicePlus 440