[英]How to display the output of a program both to a file and to the console(stdout)?
假設我有以下只輸出“Hello World”的程序:
//DEMO.c
#include<stdio.h>
int main()
{
printf("HELLO World");
}
現在我想將它顯示在屏幕和文件output.txt
。所以我在命令提示符下輸入以下命令(我在Windows XP上使用CodeBlocks並將其配置為在命令提示符下工作):
demo.exe>>output.txt>>stdout
它不起作用! 請告訴我怎么做,即如何輸出我在屏幕上看到的相同的東西(當我運行程序時),同時輸出到文本文件?
#include <stdio.h>
#define my_fprintf(fp,...) do{fprintf(fp, __VA_ARGS__);fprintf(stdout, __VA_ARGS__);}while(0)
int main(int argc, char **argv){
FILE *fp;
fp=fopen("output.txt","w");//or filename from argv[1]
my_fprintf(fp, "hello world by %s\n", argv[0]);
fclose(fp);
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.