If any error occurs, an error indicator for the stream will be set and will not be cleared before the call clearerr. However, due to buffering, it is difficult for stdio functions to report errors. Often, the error will not be visible until a later call, because the buffering data never fails, but subsequent recording after filling the buffer may fail. If you use stdio to write files, the only ways I know how to handle write errors correctly (select one):
- disable buffering (with
setbufor setvbuf- this should be the very first operation performed on FILEafter it is opened or else it has UB) - track the last successful one
fflushand accept any data after that, perhaps only partially written
, POSIX fwrite ( stdio) errno , . C, errno, .