我可以将stdout重定向到文件和控制台。 [英] Can I redirect stdout to a file AND the console.

查看:162
本文介绍了我可以将stdout重定向到文件和控制台。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个程序,将大量信息写入stdout

(和stderr)。从命令行运行时,此输出在控制台窗口上显示为

(默认值),或者可以通过

表示freopen()函数重定向到文件:


freopen(" logfile.txt"," w",stdout);


但是我想要输出控制台和日志文件。这可能是

(不使用额外的程序,比如''tee'')?

I wrote a program that writes a large amount of information to stdout
(and stderr). When run from the commandline, this output either appears
on the console window (the default) or can be redirected to a file by
means of the freopen() function:

freopen ("logfile.txt", "w", stdout);

But I would like to have the output on the console AND a logfile. Is
this possible (without using an extra program like ''tee'')?

推荐答案

Jef Driesen< je********@hotmail.com.invalidwrote:
Jef Driesen <je********@hotmail.com.invalidwrote:

我写了一个程序,将大量信息写入stdout

(和stderr)。从命令行运行时,此输出在控制台窗口上显示为

(默认值),或者可以通过

表示freopen()函数重定向到文件:


freopen(" logfile.txt"," w",stdout);


但是我想要输出控制台和日志文件。这可能是

(不使用像''tee''这样的额外程序)?
I wrote a program that writes a large amount of information to stdout
(and stderr). When run from the commandline, this output either appears
on the console window (the default) or can be redirected to a file by
means of the freopen() function:

freopen ("logfile.txt", "w", stdout);

But I would like to have the output on the console AND a logfile. Is
this possible (without using an extra program like ''tee'')?



并非没有写两次。


Richard

Not without writing it twice.

Richard


在文章< fi ********** @ ikaria.belnet.be> ;, Jef Driesen

< je ******** @ hotmail.com.invalidwrote 2007年11月23日星期五下午1:17:
In article <fi**********@ikaria.belnet.be>, Jef Driesen
<je********@hotmail.com.invalidwrote on Friday 23 Nov 2007 1:17 pm:

我写了一个程序,将大量信息写入stdout

(和stderr )。从命令行运行时,控制器窗口上显示此输出

(默认值),或者可以通过freopen()函数重定向到

文件:


freopen(" logfile.txt"," w",stdout);


但是我想要输出控制台和日志文件。这可能是

(不使用像''tee''这样的额外程序)?
I wrote a program that writes a large amount of information to stdout
(and stderr). When run from the commandline, this output either
appears on the console window (the default) or can be redirected to a
file by means of the freopen() function:

freopen ("logfile.txt", "w", stdout);

But I would like to have the output on the console AND a logfile. Is
this possible (without using an extra program like ''tee'')?



是的。将输出复制到stderr和文件。你可以使用一个

小的包装器。用于封装此功能的函数。


无论如何,您需要使用两个不同的文件流。您不能通过写入

标准C中的相同流来将

输出路由到多个设备。可能有平台特定功能,但

你需要在一个适合你系统的小组中询问。

Yes. Duplicate your output to both stderr and to the file. You can use a
small "wrapper" function to encapsulate this functionality.

In any case you need to use two different file streams. You can''t route
output to multiple devices by writing to the same stream within
Standard C. It might be possible with platform specific functions, but
you need to ask in a group appropriate for your system.


santosh说:
santosh said:

文章< fi ********** @ ikaria.belnet.be> ;, Jef Driesen

< je ******** @ hotmail .com.invalidwrote于2007年11月23日星期五下午1:17:
In article <fi**********@ikaria.belnet.be>, Jef Driesen
<je********@hotmail.com.invalidwrote on Friday 23 Nov 2007 1:17 pm:



< snip>

<snip>


>>
但我想在控制台和日志文件上输出。这是否可能(不使用像''tee''这样的额外程序)?
>>
But I would like to have the output on the console AND a logfile. Is
this possible (without using an extra program like ''tee'')?



是的。将输出复制到stderr和文件。你可以使用一个

小的包装器。用于封装此功能的函数。


Yes. Duplicate your output to both stderr and to the file. You can use a
small "wrapper" function to encapsulate this functionality.



也许是这样的(但请看下面的注释):


#include< stdio.h>

#include< stdarg.h>


int tfprintf(FILE * fpa,FILE * fpb,const char * fmt,...)

{

int rc = 0;

va_list ap = {0};

va_start(ap,fmt);

rc = vfprintf(fpa,fmt,ap);

va_end(ap);

if(rc> = 0)

{

va_start(ap,fmt);

rc = vfprintf(fpb,fmt,ap);

va_end (ap);

}

返回rc;

}


这个函数''处理来自两个vfprintf调用的返回值

并不是特别令人满意,但是很难看出如何使用一个解决方案来获得
让每个人都感到高兴。这是一个

的可能性:


int tfprintf(int * rcb,FILE * fpa,FILE * fpb,const char * fmt,... )
(函数本身内部有明显的变化),因此函数

返回第一个vfprintf返回的值,并且* rcb已填充

,第二个vfprintf返回的值。


这是另一个解决方案:


struct tfprintf_rt_

{

int rca;

int rcb;

};


struct tfprintf_rt_ tfprintf(FILE * fpa,FILE * fpb,const char * fmt,...)

这里是另一个:

#define TFPRINTF_BOTH_OK 0

#define TFPRINTF_FAIL1 1

#define TFPRINTF_FAIL2 2

#define TFPRINTF_BOTH_BAD(TFPRINTF_FAIL1 | TFPRINTF_FAIL2)


int tfprintf(struct tfprintf_rt_ * rc,FILE * fpa,FILE * fpb,const char

* fmt,...)


,返回值给出一个快速和脏的bitflag形式的摘要,

,如果需要的话,详细信息存储在结构中。


但他们都很糟糕,真的,不要他们呢?这只是找到一个糟糕*最少*的

方法的问题,无论是这些还是其他一些

构造中的一个 - 这是非常的个人风格的选择。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

Something like this, perhaps (but see the note that follows):

#include <stdio.h>
#include <stdarg.h>

int tfprintf(FILE *fpa, FILE *fpb, const char *fmt, ...)
{
int rc = 0;
va_list ap = {0};
va_start(ap, fmt);
rc = vfprintf(fpa, fmt, ap);
va_end(ap);
if(rc >= 0)
{
va_start(ap, fmt);
rc = vfprintf(fpb, fmt, ap);
va_end(ap);
}
return rc;
}

This function''s handling of the return values from the two vfprintf calls
is not particularly satisfactory, but it''s hard to see how one could come
up with a solution that would be pleasing to everyone. Here''s one
possibility:

int tfprintf(int *rcb, FILE *fpa, FILE *fpb, const char *fmt, ...)

(with the obvious changes within the function itself), so that the function
returns the value returned by the first vfprintf, and *rcb is populated
with the value returned by the second vfprintf.

Here''s another solution:

struct tfprintf_rt_
{
int rca;
int rcb;
};

struct tfprintf_rt_ tfprintf(FILE *fpa, FILE *fpb, const char *fmt, ...)

And here''s another:

#define TFPRINTF_BOTH_OK 0
#define TFPRINTF_FAIL1 1
#define TFPRINTF_FAIL2 2
#define TFPRINTF_BOTH_BAD (TFPRINTF_FAIL1 | TFPRINTF_FAIL2)

int tfprintf(struct tfprintf_rt_ *rc, FILE *fpa, FILE *fpb, const char
*fmt, ...)

with the return value giving a quick and dirty summary in bitflag form,
with the details stored in the struct for perusal if required.

But they all suck, really, don''t they? It''s just a matter of finding the
method that sucks *least*, whether it is one of these or some other
construction - and that''s very much a personal style choice.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


这篇关于我可以将stdout重定向到文件和控制台。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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