簡體   English   中英

如何將程序的輸出顯示到文件和控制台(stdout)?

[英]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

它不起作用! 請告訴我怎么做,即如何輸出我在屏幕上看到的相同的東西(當我運行程序時),同時輸出到文本文件?

您需要為Windows下載tee命令。 tee是一個UNIX / Linux命令,它將標准輸入復制到標准輸出並輸出到文件。 然后,你可以這樣做:

demo.exe | tee output.txt

這是 Windows的一個tee端口

#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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM