*BSD News Article 65893


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!news.sol.net!daily-planet.execpc.com!homer.alpha.net!solaris.cc.vt.edu!hookup!tank.news.pipex.net!pipex!dispatch.news.demon.net!demon!awfulhak.demon.co.uk!awfulhak.demon.co.uk!not-for-mail
From: brian@awfulhak.demon.co.uk (Brian Somers)
Newsgroups: comp.unix.bsd.misc,alt.unix.wizards,comp.unix.misc
Subject: Re: How to write end of file character into file??!
Followup-To: comp.unix.bsd.misc,alt.unix.wizards,comp.unix.misc
Date: 16 Apr 1996 01:13:36 +0100
Organization: Coverform Ltd.
Lines: 58
Message-ID: <4kuonh$q2@anorak.coverform.lan>
References: <xsvarshney-0604962038290001@newshub.csu.net> <4kb6sv$399@is05.micron.net> <4khh63$a3@anorak.coverform.lan> <4ktvmc$5d6@is05.micron.net>
NNTP-Posting-Host: anorak.coverform.lan
X-NNTP-Posting-Host: awfulhak.demon.co.uk
X-Newsreader: TIN [version 1.2 PL2]
Xref: euryale.cc.adfa.oz.au comp.unix.bsd.misc:729 alt.unix.wizards:3646 comp.unix.misc:22006

Dwight Tovey (dwight@micron.net) wrote:
: In article <4khh63$a3@anorak.coverform.lan>, brian@awfulhak.demon.co.uk (Brian Somers) writes:
: |> Dwight Tovey (dwight@micron.net) wrote:
: |> [stuff deleted]
: |> 
: |> : data_f = fopen( "input", "r" );
: |> : while( !feof( data_f )) {
: |> :   fread(....)
: |> :    .....
: |> : }
: |> 
: |> : fclose( data_f );
: |> 
: |> Nope, you'd be better off writing
: |> 
: |> data_f = fopen( "input", "r" );
: |> while( fread(....), !feof( data_f ) ) {
: |>    .....
: |> }
: |> 
: |> fclose( data_f )
: |> 
: |> And don't ever call me pedantic ! :-)
: |> 

: Not only are you pedantic, you are wrong. :)
: With what you have, you won't process the last block of data if it is
: smaller than the size of the read request.

: The code I had wasn't complete either, but my intent was only to bring
: attention to the feof macro.
: 	/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!!
:  

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