将标准输出重定向到文件而不显示 ANSI 警告

2023-11-30

我一直在尝试将程序的 STDOUT 重定向到文件。到目前为止,这段代码运行良好:

FILE *output = fopen("output","w");
if (dup2(fileno(output),1) == -1)
{
    /* An error occured. */
    exit(EXIT_FAILURE);
}

问题是,我试图坚持使用 ANSI C,而 fileno 不是 ANSI。当我用 gcc 编译时,我收到警告:

gcc -Wall -ansi -pedantic
warning: implicit declaration of function ‘fileno’

有没有办法将 STDOUT 重定向到 ansi C 中的文件?


ANSI C 的方法是freopen():

if (freopen("output", "w", stdin) == NULL) {
    /* error occured */
    perror("freopen");
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将标准输出重定向到文件而不显示 ANSI 警告 的相关文章

随机推荐