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

前端 未结 13 1247
梦如初夏
梦如初夏 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条回答
  •  春和景丽
    2021-01-02 12:10

    Something like this would work

    if (fwrite (&blah, sizeof (blah), 1, fp) != 1) throw SomeException;
    

    If you're worried about pointers being cleaned up you can wrap the pointer in some form of smart pointer before carrying out your fwrite's.

    If you don't want to use smart pointers then this will work, but it's messy, so I'd try the smart pointer route first

    if (fwrite (&blah, sizeof (blah), 1, fp) != 1) {
        //cleanup here
        throw SomeException;
    }
    

提交回复
热议问题