What is a good programming pattern for handling return values from stdio file writing functions

前端 未结 13 1253
梦如初夏
梦如初夏 2021-01-02 11:28

I\'m working on some code that generates a lot of

ignoring return value of ‘size_t fwrite(const void*, size_t, size_t, FILE*)’, declared with attribute warn         


        
13条回答
  •  旧时难觅i
    2021-01-02 11:56

    Well ... You could create a wrapper function, that re-tries the write if it fails, perhaps up to some maximum number of retries, and returns success/failure:

    int safe_fwrite(FILE *file, const void *data, size_t nbytes, unsigned int retries);
    void print_and_exit(const char *message);
    

    Then your main code could be written as

    #define RETRIES 5
    if(!safe_fwrite(fp, &blah, sizeof blah, RETRIES))
      print_and_exit("Blah writing failed, aborting");
    if(!safe_fwrite(fp, &foo, sizeof foo, RETRIES))
      print_and_exit("Foo writing failed, aborting");
    

提交回复
热议问题