In standard C, how do you reliably check whether all output that was written to a standard I/O stream was successfully saved to disk?
The C standard states that fclose will return 0 on success, or EOF if "any errors were detected".
But does that mean "any errors were detected during the fclose call"? Or does it mean "any errors were detected since the last call to clearerr"?
In other words, is it sufficient for a program to just check the return value of fclose, or is it also necessary to check ferror? Are there any implementations where, if ferror returns non-zero, a subsequent call to fclose might return 0?
The same goes for fflush: is it always the case that if fflush returns 0, a subsequent call to ferror will also return 0, and if fflush returns EOF, a subsequent call to ferror will return non-zero? Are there any implementations for which this is not the case?
(Of course, I'm not considering power outages, gremlins, etc. Yes, a program that requires guaranteed durability should use fsync, but that's outside the scope of standard C.)