Re: Behavior of feof()

Lars Wirzenius (liw@iki.fi)
Sun, 23 Feb 1997 21:07:36 +0200


--==_Exmh_-1975293400P
Content-Type: text/plain; charset=us-ascii

[ Please don't Cc: me when replying to my message on a mailing list. ]

Russell Berry:
> Okay, that's enough of that, Here is ANSI C Paragraph 7.9.10.2

Please also read 7.9.1 (description of FILE), 7.9.3 (last
sentence in first paragraph), and 7.9.7.1 (fgetc). See also
the C FAQ at <URL:http://www.eskimo.com/~scs/C-faq/top.html>;
all of it.

You don't use feof to predict whether the next read will hit
end-of-file; you use it to see whether a previous operation
already did hit end-of-file. Your program was, essentially:

> color=fopen("XColor.java","w+");
> while(!feof(rgb)){
> fscanf(rgb,"%d%d%d%s",&r,&g,&b,cname);
> fprintf(color," ////////////////////\n");
> fprintf(color," // The color %s\n",cname);
> fprintf(color," ////////////////////\n\n");
> fprintf(color," public final static Color %s = new\
> Color(%d, %d, %d);\n\n",cname,r,g,b);
> }

You are calling feof to see whether the fscanf will succeed. That
is wrong.

You have an infinite loop because for some reason fscanf
never sets the end-of-file indicator. This is almost
certainly because your input file doesn't match the fscanf
pattern. I seriously doubt it is because fscanf or feof is
buggy. If you think they are, take the thread to linux-gcc, or
comp.os.linux.development.system, and provide the problematic
input file (a couple of lines is enough), and version numbers
for all relevant parts of the software. This really isn't
on-topic for linux-kernel.

Oh, and publishing private e-mail is quite rude, thank you very much.

-- 
Please read <http://www.iki.fi/liw/mail-to-lasu.html> before mailing me.
Please don't Cc: me when replying to my message on a mailing list.

--==_Exmh_-1975293400P Content-Type: application/pgp-signature

-----BEGIN PGP MESSAGE----- Version: 2.6.3i

iQCVAwUBMxCVdIQRll5MupLRAQGzIgP/RlylkZYpzASJzZ5/PIYl/xgii6K1IEGS UFZjRS3HMVE4HFlxaJ0XacYuLcPkAr6KvBl3SYY32i3x2dGKPZrUzKOU8VTykPyQ gpNRlN+XzQ5nABcoCKZ4bOuUNSiZu/jqAiULIH7qyrg7tO2svRtXaL2/Wm+4C5dB T6UL13Jwj6c= =X9ZT -----END PGP MESSAGE-----

--==_Exmh_-1975293400P--