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!swrinde!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: 22 Apr 1996 15:45:12 GMT Organization: Wish I had some Lines: 88 Message-ID: <4lg9i8$8if@is05.micron.net> 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> 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:774 alt.unix.wizards:3675 comp.unix.misc:22106 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. |> |> -- |> Mark J. Kaufman, Manager, UNIX/PDE Projects |> mark.kaufman@sandiegoca.attgis.com |> (619) 485-3891 VoicePlus 440 Are you always this abrasive to people or do you reserve it for Usenet. Nice attitude for a "Manager". 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. 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. /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!!