### Question 12.2

Why does the code `while(!feof(infp)) { fgets(buf, MAXLINE, infp); fputs(buf, outfp); }` copy the last line twice?

In C, `EOF` is only indicated after an input routine has tried to read, and has reached end-of-file. (In other words, C's I/O is not like Pascal's.) Usually, you should just check the return value of the input routine (`fgets` in this case); often, you don't need to use `feof` at all.

References: K&R2 Sec. 7.6 p. 164
ANSI Sec. 4.9.3, Sec. 4.9.7.1, Sec. 4.9.10.2
ISO Sec. 7.9.3, Sec. 7.9.7.1, Sec. 7.9.10.2
H&S Sec. 15.14 p. 382

Read sequentially: prev next up top

This page by Steve Summit // Copyright 1995 // mail feedback