[prog] C problem I am unable to solve

Colleen Hatfield evilpig at gmail.com
Fri May 13 03:17:54 EST 2005


On 5/12/05, Colleen Hatfield <evilpig at gmail.com> wrote:
> On 5/12/05, Dan <dan at cellectivity.com> wrote:

> > I think the problem is here:
> >
> > >      while ( (c = getc(inputfile)) != '\n' )
> > >         putc(c, stdout); /* display the line */
> >
> > EOF is not '\n'     :-)

Sorry to reply to my reply, and to add list noise, but it just clicked
to me what Dan was saying (I thought he was suggesting something
entirely different), and he is of course correct, and that is exactly
where the infinite loop is occurring.

For anyone else that would be helped by a more verbose explanation:

Program executes normally, printing lines and prompting for user
input.  When that inner loop to read and print the lines reaches the
end of the file and encounters the EOF, it loops infinitely reading
and printing EOF because that loop will not exit until it encounters
the next "\n", which never happens.

Changing that line to:
     while ( (c = getc(inputfile)) != '\n' && c != EOF)
fixes the issue.

Sorry for confusing the issue :-(

- Colleen


More information about the Programming mailing list