title: What is a good programming pattern for handling return values from stdio file writing functions
body: <p>I'm working on some code that generates a lot of </p>
  
  <pre><code>ignoring return value of ‘size_t fwrite(const void*, size_t, size_t, FILE*)’, declared with attribute warn_unused_result
  </code></pre>
  
  <p>warnings when compiled with g++, and I'm wondering about the best programming pattern to actually record and handle the return value of a large number of separate sequential <code>fwrite</code>s (i.e. not the same <code>fwrite</code> in a loop)</p>
  
  <p>Let's say that the code looks like this at the moment:</p>
  
  <pre><code>fwrite (&amp;blah, sizeof (blah), 1, fp);
  // ... more code ...
  fwrite (&amp;foo, sizeof (foo), 1, fp);
  // ... more code ...
  </code></pre>
  
  <p>I'm currently thinking about something like this, but I may have difficulty cleaning up the file pointer:</p>
  
  <pre><code>if (fwrite (&amp;blah, sizeof (blah), 1, fp) != 1) return someerrorcode;
  // ... more code ...
  if (fwrite (&amp;foo, sizeof (foo), 1, fp) != 1) return someerrorcode;
  // ... more code ...
  </code></pre>
  
  <p>I think that approach is clearly better than nesting, which would get too crazy too quick:</p>
  
  <pre><code>if (fwrite (&amp;blah, sizeof (blah), 1, fp) == 1) {
     // ... more code ...
     if (fwrite (&amp;foo, sizeof (foo), 1, fp) == 1) {;
        // ... more code ...
     }
  }
  </code></pre>
  
  <p>Surely there is already an established best-practice pattern for this sort of thing, though?</p>
  
  <p>Of course, as I am mainly looking into this to get rid of the compiler warnings, I could just assign the return value to a dummy variable and ignore it, but I'd like to try doing it the right way first.</p>
  
  <pre><code>dummy = fwrite (&amp;blah, sizeof (blah), 1, fp);
  // ... more code ...
  dummy = fwrite (&amp;foo, sizeof (foo), 1, fp);
  // ... more code ...
  </code></pre>
  
  <p><strong>Update:</strong> I've removed the c++ tag as this code is really just c being compiled using g++, so c based solutions are needed to keep with the rest of the code base.</p>