fgets

Henry Spencer henry at utzoo.UUCP
Fri Apr 11 08:59:39 AEST 1986


> ...So why doesn't fgets stick
> a '\0' in the buffer when it sees EOF immediately? Isn't this a bug?
> What I did to fix it was to set line_buffer[0]=NUL *before* calling
> fgets, which is simple enough to do. Still.... grumble, grumble...

Probably the right answer to this is that you should be checking the
return value from fgets, rather than consulting feof separately.  The
semantics of the return value aren't well explained in the manual, but
the code is doing the right thing:  you get NULL back only if there was
*nothing* available on the input.  If there's a partial line at the end
of the file, you get that partial line and a non-NULL return, and then
*next* time you get a NULL.

It is widely agreed that the details of the semantics of fgets could
have been done better.
-- 
				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,decvax,pyramid}!utzoo!henry



More information about the Comp.lang.c mailing list