In a test, I'm discarding anything from stderr
since it clutters the output of the test case. I'm using the following code:
freopen("/dev/null", "w", stderr);
When compiling with -Wall -Werror
, I get the error
error: ignoring return value of ‘freopen’, declared with attribute warn_unused_result
which is expected. However, the usual solution of casting to void
doesn't seem to work. That is, changing the code to
(void) freopen("/dev/null", "w", stderr);
still produces the same warning. I don't care if this function fails since the worst case scenario is a bit of extra output. Any other way I can fix this?
EDIT: I know I could introduce an extra unnecessary variable. I would really like to know why casting to void doesn't work.
UPDATE: I decided to go with this:
FILE *null = fopen("/dev/null", "w");
if (null) { fclose(stderr); stderr = null; }
After reading the freopen
documentation more carefully, I see that if opening /dev/null
fails, stderr
will still be destroyed. This solves that problem.