- EOF compared to a character
int getword(...)
{
char c;
int i = 0;
...
while (((c = getc(fp)) != EOF) && (first(c) == 0));
...
}
- missing test for EOF
while ((c = getc(fp)) && (rest(c) != 0) && (i < SIZE - 1))
buf[i++] = c;
- do tests in correct order
...
c = fgetc(fp);
while((first(c) == 0) && c != EOF)
c = fgetc(fp);
...
- do range tests at appropriate times
...
/* Read rest */
while (((c = fgetc (fp)) != EOF) && (rest(c) != 0))
buf[i++] = c;
if (i > size - 1) i = size;
/* Check if length is over MAX, and return */
...
- you can only store size-1 characters!
- if size == 1 you cannot store any character!
...
c = fgetc (fp);
while ((c != EOF) && (!first(c)))
c = fgetc(fp);
if (c==EOF)
return EOF;
buf[0]=c;
i = 1;
...
- first(c) doesn't necessarily imply rest(c).
...
while (first(ctemp)!=1){
ctemp=fgetc(fp);
if (ctemp==EOF)
return EOF;
}
while (rest(ctemp)==1){
...
- Don't forget to skip the remainder of a word if it doesn't fit
into the buffer!
- The first character that doesn't belong to the word anymore must
be pushed back onto the input stream using ungetc. Be
careful not to apply ungetc to EOF!