设置FILE*是否等于stdout portable?

Ful*_*igh 5 c stdout

我有一个函数需要能够写入stdout或文件,具体取决于用户想要的.它默认为标准输出.为了实现这一点,我正在做以下(减去错误检查等):

FILE* out;
if (writeToFile) { /*Code to open file*/; }

else
    out = stdout;

// ...rest of the function goes here

if (out != stdout)
    fclose(out);
Run Code Online (Sandbox Code Playgroud)

这当然可以解决问题,但我不知道它有多便携.如果它不是,和/或它还有另一个问题,我应该怎么做呢?

Ker*_* SB 8

是的,它是可移植的并且很好,前提是你还没有搞乱低级实现*stdout(例如通过调用close(fileno(stdout))Posix或使用dup).