将标准输出重定向到没有 ANSI 警告的文件 [英] Redirect stdout to file without ANSI warnings
本文介绍了将标准输出重定向到没有 ANSI 警告的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直试图让程序的 STDOUT 重定向到一个文件.到目前为止,这段代码运行良好:
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);
}
问题是,我试图坚持使用 ANSI C,而 fileno 不是 ANSI.当我用 gcc 编译时,我收到警告:
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’
有没有办法将 STDOUT 重定向到 ansi C 中的文件?
Is there any way at all to redirect STDOUT to a file in ansi C?
推荐答案
ANSI C 的做法是 freopen()
:
The ANSI C way to do it is freopen()
:
if (freopen("output", "w", stdin) == NULL) {
/* error occured */
perror("freopen");
}
这篇关于将标准输出重定向到没有 ANSI 警告的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文