You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Calls to fprintf() will never return an error. They always "succeed", returning the number of bytes that should have been written.
Hardware or other errors may not allow for the actual write of the data in the physical medium, but fprintf() is unable to detect this.
Here is a relevant discussion in the mailing list.
I think that there are two major cases of fprintf() usage:
Buffered Output
When the output is buffered, naturally fprintf() will not be able to tell whether there will be a failure.
It only writes the data to a buffer, and then the FS will dump them to the physical medium (at a later time).
In this case it seems normal that it will not return any errors, I don't think that we can do anything about this case.
Unbuffered Output
In this case, fprintf() will directly write the data to the FS.
Now it is able to detect any errors, but currently it just swallows them.
I believe that this case can be improved, so errors do propagate to the user.
The errors are known to fprintf() and have already happened, in contrast to the case using buffers.
I don't see any reason to lose this information.
Currently, a work-around is to use syncfs() and check this for errors:
FILE*f;
syncfs(fileno(f))
This will work for both buffered & unbuffered writes.
Activity
xiaoxiang781216 commentedon Aug 11, 2023
Unbuffered write mayn't return error too, since file system internal or block driver may cache the data.
This code will run very slow, if you call synfs after every fprintf.