fgets

Karl Heuer karl at haddock.ima.isc.com
Thu Oct 25 13:53:47 AEST 1990


In article <576 at inews.intel.com> bhoughto at cmdnfs.intel.com (Blair P. Houghton) writes:
>In article <52039 at unix.cis.pitt.edu> ydist at unix.cis.pitt.edu (Yijun Ding) writes:
>>fputs("", fp) return 0 in TC 2.0
>>              return (-1) in TC++
>>
>>Which is correct ?
>
>Under ANSI, the return value on error from fputs() is an EOF, the
>return value if no error is "nonnegative".  [Perhaps some nonstandard
>system is using 0 for failure.]

I don't think so.  It looks to me as though both implementations agree that -1
means failure, but it's questionable whether a zero-length write is a failure
or a trivial success.  (There are all sorts of dark corners when dealing with
zero-sized objects; the answer can be "operation fails", "operation succeeds",
or "behavior undefined" depending on the details.)  If the Standard isn't
clear on this (sorry, I can't look it up just now), one can always send X3J11
a request for interpretation.

The pragmatic solution is to rewrite the code so it doesn't matter:
	if (*s != '\0' && fputs(s, fp) == EOF) panic();

Karl W. Z. Heuer (karl at ima.isc.com or uunet!ima!karl), The Walking Lint



More information about the Comp.lang.c mailing list