Author: Dann Corbit
Date: 22:44:38 03/28/03
Go up one level in this thread
On March 29, 2003 at 01:30:44, TEERAPONG TOVIRAT wrote:
>Hi,
>
>I don't understand how feof() works.
>I stored book positions(hashvalue) in a binary file. Each time I read the file
>to
>the end there would be one more integer at the end of file. Is this a normal
>feature?
From the C-FAQ:
12.2: Why does the code
while(!feof(infp)) {
fgets(buf, MAXLINE, infp);
fputs(buf, outfp);
}
copy the last line twice?
A: In C, end-of-file is only indicated *after* an input routine has
tried to read, and failed. (In other words, C's I/O is not like
Pascal's.) Usually, you should just check the return value of
the input routine (in this case, fgets() will return NULL on end-
of-file); often, you don't need to use feof() at all.
References: K&R2 Sec. 7.6 p. 164; ISO Sec. 7.9.3, Sec. 7.9.7.1,
Sec. 7.9.10.2; H&S Sec. 15.14 p. 382.
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.