Ç改性的printf(),以输出到文件 [英] C modifying printf () to output to a file
问题描述
是否有修改的printf
,以输出字符串上的文件,而不是到控制台?
Is there a way to modify the printf
in order to output string on a file rather than to the console?
我试着在网上找一些东西,发现类似通话DUP
, dup2
和> fflush可能与此有关。
I tried looking up something on the Internet and found calls like dup
, dup2
and fflush
that might be associated with this.
编辑:
也许我并不清楚..事情是,这是在C考试的问题..问题如下:
Maybe I wasn't clear.. the thing is that this was in a C exam question.. the question is as follows:
如何解释,通常输出字符串到屏幕上的程序(使用的printf()
),可向输出字符串到文件中,无改变任何code在提到计划。
Explain how a program that normally output strings to screen (using printf()
) can be made to output string to a file, without changing any code in the mentioned program.
推荐答案
这通常与I / O重定向(...>文件)完成的。
This is usually done with I/O-redirection (... >file).
检查这个小程序:
#include <stdio.h>
#include <unistd.h>
int main (int argc, char *argv[]) {
if (isatty (fileno (stdout)))
fprintf (stderr, "output goes to terminal\n");
else
fprintf (stderr, "output goes to file\n");
return 0;
}
ottj@NBL3-AEY55:~ $ ./x
output goes to terminal
ottj@NBL3-AEY55:~ $ ./x >yy
output goes to file
这篇关于Ç改性的printf(),以输出到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!