[prog] C problem I am unable to solve

Angelina Carlton brat at magma.ca
Fri May 13 00:48:03 EST 2005


On Wed, May 11, 2005 at 10:47:52PM +0200, Almut Behrens wrote:

> I think Dan has already spotted the main problem...
> So, I'd just like to add a little comment on the loop structure:
 
> Alternatively, you could write
> 
>   while(...) {
>     for (i = 0; i <= 19; ++i) {
>     }
>     /* wait for keypress */
>   }

I have altered it now to read:

  while ( ((c = getc(inputfile)) != EOF)) {
    for (i = 0; i <= 19; ++i) {
      while ( (c = getc(inputfile)) != '\n' )
        putc(c, stdout); /* display the line */
      putc('\n', stdout);
    }
    printf("press any key to quit, or [ENTER] to continue:\n");
    while ((q = getchar()) != '\n' && q != EOF  ) {
      return 0;
    }
  }
  return 0;
}

The problem I have now is an infinate loop once it reaches the end of
the file. Even using feof() I still get that problem, c is declared as
an int, which from what I have read is correct, fgets,getchar,getc all
return an int when they find the EOF so according to what I have
understood this is the "correct" way to check for the end of a file?

It has occured to me that perhaps something else is causing the loop
and not the EOF, I don't know what else to check for though.
 
-- 
Angelina Carlton


More information about the Programming mailing list