Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我的程序使用两个参数(放入argv)执行,如下所示:
argv
$ myProgram input output
如何将所有内容重定向printf(..)到输出文件?我看到了一些关于使用的建议,fflush(stdout)但我以前没有使用过。谁能给我举个例子?
printf(..)
fflush(stdout)
如果您尝试重定向程序的输出,那么可以从命令行本身轻松完成,而无需向程序添加任何其他代码。只需像这样修改命令。
$ myProgram input output > example.txt
如果您想将输出附加到输出文件的末尾,那么命令将是这样的。
$ myProgram input output >> output
但是,在这两种情况下,屏幕上都不会打印任何内容。程序的整个输出将写入文件中。
你将不得不你fprintf()而不是printf
fprintf()
printf
这是一个例子
#include <stdio.h> main() { FILE *fp; fp = fopen("/tmp/test.txt", "w+"); fprintf(fp, "This is testing for fprintf...\n"); fputs("This is testing for fputs...\n", fp); fclose(fp); }
有关更多详细信息,请阅读此页面 和此页面 此代码取自第一个链接。