B
Barry Schwarz
That's not quite true. There are certainly explicit requirements for the
arguments passed to fgets(), lest UB occur.
I agree that bad arguments can cause UB but that's true for any
function. fgets appears in section J.2 only once (regarding an
attempt to use the array if fgets reports an I/O error) and that
section is not specific to fgets but includes other I/O functions.
I prefer to think the behaviour on reading the last line of text is
implementation defined.
Implementation-defined implies there is more than one possible legal
behavior. If you think it is implementation defined, does your
implementation document it and, if so, as what? What other possible
behaviors are there that satisfy the standard? Can fgets return
something other than the array address? If the '\n' is not present in
the stream, can it stop reading before the end of file? Can it fail
to terminate the string with a '\0'? Can it add a '\n' of its own?
Now that I think about it some more, I think this should be required
to be detected as an I/O error rather than undefined behavior.
I don't disagree, but sanity would dictate that there's no need to lump
fgets() into the same basket as gets().
I don't follow this. How does discussing what fgets does when it
processes an improperly formatted stream "lump it with gets"?
[Aside: Dealing with null bytes via fgets() is much trickier than trailing
newlines. Throw in non-sticky EOF and ...]
The standard does not indicate that fgets treats a '\0' in the input
stream different than any other non-'\n' character. In fact, there
are only three conditions (other than I/O error) that will terminate
the operation:
'\n' encountered in the stream
end of file encountered in the stream
n-1 characters read from the stream
What is a non-sticky EOF?
What does this have to do with how fgets processes the last line of a
text file?
ITYM: that is not specific to fgets...
Yes.
<<Remove the del for email>>