On Thu, 27 Oct 2011 21:08:55 -0400, Keith Thompson <
ks...@mib.org> wrote:
> "Bill Cunningham" <nos...@nspam.invalid> writes:
>> Oop. Sorry for the trouble. I need to start paying attention more to my
>> docs. I see the comparison problem now.
>
> By all means, don't bother to tell us what the problem was. No point in
> anyone else learning anything.
>
Bill implied that the error was in the line:
> if ((fp = fopen(argv[1], "r+")) == EOF) {
where, of course, he's comparing a (FILE *) to an (int)
...
Also providing some amusement is the code
> for (; i != EOF; ++i) {
> fgetc(fp);
> fputc(a, fp);
> return 0;
> }
which attempts to write a single nul byte somewhere in the file fp, and
then exit. However, unless the file was initially empty, this code
violates a constraint, since input (fgetc) is followed immediately by
output (fputc) without any intervening file-positioning operation.
(7.19.5.3, paragraph 6).
Bill seems to be off his meds again, and I have no real idea what this
code was intended to do. Even if he meant to iterate through the loop
until ((a = fgetc(fp)) == EOF), I can't guess what he intended by
writing a back to the same file.
The body of the "loop" is only executed once, which is a good thing,
otherwise it would run through thousands of iterations, at least, until
i == INT_MAX, at which point incrementing invokes (undefined? unspecified?)
behavior. On the platforms I'm most familiar with, I think that adding
one to INT_MAX is likely to result in INT_MIN, and thousands of iterations
later, i would eventually equal -1, which is the usual definition of EOF.
--
Morris Keesan --
mke...@post.harvard.edu