I've got a function that needs to be able to write to either stdout, or to a file, depending on what the user wants. It defaults to standard out though. To accomplish this, I'm doing the following (minus error checking etc):
FILE* out;
if (writeToFile) { /*Code to open file*/; }
else
out = stdout;
// ...rest of the function goes here
if (out != stdout)
fclose(out);
This certainly does the trick, but I have no idea how portable it is. And if it's not, and/or there's another problem with it, how should I go about this?