Trouble at EOF
David Tanguay
datangua at watmath.waterloo.edu
Mon Jun 17 22:09:27 AEST 1991
In article <4739 at inews.intel.com> bhoughto at pima.intel.com (Blair P. Houghton) writes:
> while ( fgets(s, sizeof s, stream) )
> /* not eof */
> process(s);
>
> /* reached iff eof */
> if ( strlen(s) != 0 ) {
> /* there's something left to process */
> if ( s[strlen(s) - 1] != '\n' )
> /* it has no newline */
> strcat(s,"\n");
> process(s);
> }
Huh? 4.9.7.2: "If end-of-file is encountered and no characters have been read
into the array, the contents of the array remain unchanged and a null pointer
is returned." fgets does not return NULL for eof when there are characters
read, so (barring I/O errors) the above code will process the last "line"
twice.
--
David Tanguay datanguay at watmath.waterloo.edu Thinkage, Ltd.
More information about the Comp.lang.c
mailing list