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
A potentially elegant C solution for this could be something like this (warning - untested, uncompiled code ahead):
size_t written;
int ok = 1;
size_t num_elements = x;
ok = (fwrite(stuff, sizeof(data), num_elements, outfile) == num_elements);
if (ok) {
... do other stuff ...
}
ok = ok && (fwrite(stuff, sizeof(data), num_elements, outfile) == num_elements);
if (ok) {
... etc etc ad nauseam ...
}
fclose(outfile);
return ok;
The above accomplishes two goals at the same time:
Unfortunately the 'ugly' if (ok) blocks are necessary if you don't want to use the short-circuit evaluation everywhere. I've seen this pattern used in comparatively small functions using short circuit evaluation everywhere and I would think that it's probably best suited to that particular use.