Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!henry From: henry@utzoo.UUCP (Henry Spencer) Newsgroups: net.lang.c Subject: Re: fgets Message-ID: <6579@utzoo.UUCP> Date: Thu, 10-Apr-86 17:59:39 EST Article-I.D.: utzoo.6579 Posted: Thu Apr 10 17:59:39 1986 Date-Received: Thu, 10-Apr-86 17:59:39 EST References: <2476@brl-smoke.ARPA>, <2524@utcsri.UUCP> Organization: U of Toronto Zoology Lines: 18 > ...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