A simple question, and I am about 99.99% sure of the answer but seeking some feedback so I can feel 100% confident in my understanding. When fputc
returns EOF
it is only on stream error correct? For example, say I have a very small space to write to, and the end of space is reached, fputc
would return EOF
as an error and not actually end-of-file? In other words, something like this is not required?
ch = fputc(c, stream);
if(ch == EOF) {
if(ferror(stream)) {
// error
} else {
// EOF
}
}
Because if fputc
returns EOF
it is safe for me to assume that ferror(stream)
will always return non-0?
I searched for answers on here, and I apologize if this is a duplicate question, but I am relatively new on here and haven't quite mastered the art of searching the knowledge base yet.
In ¶7.21.7.3 The fputc
function ¶3, the C standard says:
The
fputc
function returns the character written. If a write error occurs, the error indicator for the stream is set andfputc
returnsEOF
.
So, for fputc()
, your argument is correct according to the standard. If there's no space left on the device or the output stream is closed (pipe or socket) or … then an output error has occurred, and fputc()
will return EOF
(eventually, when the stream flushes its buffer) and ferror()
will report that the stream had an error.
The discussion would be different for fgetc()
and input functions.
Then ferror()
tests for an error on the file stream, but feof()
tests for EOF.
You should be wary of using either feof()
or ferror()
with input functions — notably, see while (!feof(file))
is always wrong.
Your use is acceptable — you are checking after an output function (fputc()
) returns EOF. That would also be correct if an input function reports failure — then you can check with feof()
or ferror()
.
You can use clearerr()
on a stream to clear the error and EOF indicators.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With