Skip to content

fprintf() does not return any errors. #10012

Closed

Description

@fjpanag

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

  1. xiaoxiang781216 commented on Aug 11, 2023

    @xiaoxiang781216
    Contributor

    Unbuffered Output In this case, fprintf() will directly write the data to the FS.

    Unbuffered write mayn't return error too, since file system internal or block driver may cache the data.

    Currently, a work-around is to use syncfs() and check this for errors:

    FILE * f;
    syncfs(fileno(f))

    This code will run very slow, if you call synfs after every fprintf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions