在测试中,我会丢弃任何东西,stderr
因为它会使测试用例的输出变得混乱。我正在使用以下代码:
freopen("/dev/null", "w", stderr);
编译时-Wall -Werror
出现错误
error: ignoring return value of ‘freopen’, declared with attribute warn_unused_result
这是预期的。但是,通常的强制转换解决方案void
似乎不起作用。也就是说,将代码更改为
(void) freopen("/dev/null", "w", stderr);
仍然产生相同的警告。我不在乎这个函数是否失败,因为最坏的情况是一些额外的输出。我还有其他方法可以解决这个问题吗?
编辑:我知道我可以引入一个额外的不必要的变量。我真的很想知道为什么强制转换为 void 不起作用。
更新: 我决定这样做:
FILE *null = fopen("/dev/null", "w");
if (null) { fclose(stderr); stderr = null; }
仔细阅读freopen
文档后,我看到如果打开/dev/null
失败,stderr
仍然会被销毁。这解决了这个问题。