重定向标准输出,而不ANSI警告文件 [英] Redirect stdout to file without ANSI warnings

查看:112
本文介绍了重定向标准输出,而不ANSI警告文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图让一个程序的标准输出重定向到一个文件。到目前为止,这code效果很好:

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’

有没有办法在所有标准输出重定向到一个文件中的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆