"DJP" <do***********@rediffmail.com> writes:
Hi,
I need to read a file programmatically until end of file. My logic is as
follows:
while(!feof(Fp))
FAQ. From the comp.lang.c FAQ list,
http://www.eskimo.com/~scs/C-faq/top.html :
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 -- fgets(), for example, returns NULL on end-
of-file. In virtually all cases, there's no 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.
--
Maurizio Loreti
http://www.pd.infn.it/~loreti/mlo.html
Dept. of Physics, Univ. of Padova, Italy ROT13:
yb****@cq.vasa.vg