delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/11/30/02:51:05

Date: Mon, 30 Nov 1998 09:48:57 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
X-Sender: eliz AT is
To: Christian Hofrichter <ChristianHofrichter AT gmx DOT de>
cc: djgpp AT delorie DOT com
Subject: Re: problem with fseek and/or feof
In-Reply-To: <36615BB8.7FCDE53F@gmx.de>
Message-ID: <Pine.SUN.3.91.981130094839.9230F-100000@is>
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com

On Sun, 29 Nov 1998, Christian Hofrichter wrote:

> while(feof(f)==0)
> {
>   fseek(f,1,SEEK_CUR);
> }
> fclose(f);
> 
> This program should increase the filepointer by one byte, everytime
> the loop is executed until EOF is reached.
> "feof" return 0 if the EOF is not reached. Otherwise it returns a
> value, different from 0.
> So the loop should be executed until "feof" says that EOF is reached,
> shouldn't it ?

No, it shouldn't.

You are assuming that seeking beyond the end of a file is not
possible, and yields an error.  But that assumption is not true: it is
quite possible, and makes perfect sense, to seek beyond the current
end of the file.  Both Unix and DOS/Windows allow that.  If you write
to the file after seeking beyond its end, you extend the file, and the
part in between contains binary garbage (on Unix, it is zeroed).

Since seeking is possible, fseek has no problem with any positive
offset.  And the ANSI standard says that if the seek operation is
successful, fseek clears the end-of-file indicator for the stream.
That's exactly what happens in this case, and that's why feof never
returns a nonzero value in your program.

If you need to seek to the end of the file, use SEEK_END with an
offset of zero.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019