🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.lang.c
Article #9186

Re: fgets() returns NULL at EOF??

#9186
From: Guy Harris
Date: Fri, 22 Aug 1986 08:44
24 lines
1733 bytes
> Why is it that fgets() returns NULL when it reaches end of file,
> whereas all the other standard i/o functions seem to return EOF
> at that point?

Because "fgets" returns a value of type "char *" while most of the other
functions and macros return a value of type "int".  EOF is not a valid value
of type "char *", so "fgets" can't return EOF.  NULL is a valid value of
type "char *", and doesn't refer to any object, so it's the proper choice
for an "out-of-band" value for "fgets" to return on error.

Now, you can ask "why does 'fgets' return a value of type 'char *'?"  at
this point.  It returns a pointer to the buffer that it just filled in;
obviously *somebody* found this useful, although I don't find it so.  If
"fgets" didn't return that pointer, it could have been defined as returning
a value of type "int" instead, and that value would have been 0 on success
and EOF on failure.  It's too late to change it, though.

BTW: "fgets()" returns NULL on end-of-file OR error; don't write code that
assumes that "fgets()" returning NULL means that the end of the file was
found, use "ferror" or "feof" to disambiguate these cases.
-- 
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy@sun.com (or guy@sun.arpa)
Technical Details
Message-ID: <6444@sun.uucp>
Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!lll-crg!lll-lcc!pyramid!decwrl!sun!guy
References: <3170@brl-smoke.ARPA>

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!lll-crg!lll-lcc!pyramid!decwrl!sun!guy
From: guy@sun.uucp (Guy Harris)
Newsgroups: net.lang.c
Subject: Re: fgets() returns NULL at EOF??
Message-ID: <6444@sun.uucp>
Date: Fri, 22-Aug-86 04:44:07 EDT
Article-I.D.: sun.6444
Posted: Fri Aug 22 04:44:07 1986
Date-Received: Fri, 22-Aug-86 22:01:42 EDT
References: <3170@brl-smoke.ARPA>
Organization: Sun Microsystems, Inc.
Lines: 24