将标准输出重定向到文件而不显示 ANSI 警告
我一直在尝试将程序的 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’
Is there any way at all to redirect STDOUT to a file in ansi C?
I've been trying to get a program's STDOUT redirecting to a file. So far, this code works well:
FILE *output = fopen("output","w");
if (dup2(fileno(output),1) == -1)
{
/* An error occured. */
exit(EXIT_FAILURE);
}
The issue is, I'm trying to stick to ANSI C, and fileno isn't ANSI. When I compile with gcc I get the warnings:
gcc -Wall -ansi -pedantic
warning: implicit declaration of function ‘fileno’
Is there any way at all to redirect STDOUT to a file in ansi C?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ANSI C 方法是
freopen()
:The ANSI C way to do it is
freopen()
: