简体   繁体   English

混淆stdin,stdout和stderr的工作方式

[英]Confused how stdin, stdout and stderr work

I have below program, 我有下面的程序,

void Print()
{
    printf("\nCall from Print\n");
}

int main()
{
    FILE * pFile;
    char mystring [100];

    pFile = freopen ("myfile.txt" , "r", stdin);
    if (pFile == NULL)
    {
        perror ("Error opening file");
    }
    else 
    {
        if ( fgets (mystring , 100 , pFile) != NULL )
        {
            freopen("myfile.txt" , "a", stdout);
            Print();
            printf("Here it is\n");
            //puts (mystring);
        }
        fclose(stdout);
        fclose (pFile);

    }
    printf("Hello World\n");
    return 0;
}

Now when I am execting the program I can not see the output in console window. 现在,当我执行程序时,在控制台窗口中看不到输出。 All the outputs are redirected into myfile.txt file. 所有输出都重定向到myfile.txt文件中。 I want the output should come in both console and myfile.txt too. 我希望输出也应同时出现在consolemyfile.txt

After all that why printf("Hello World\\n") is not getting printed in console. 毕竟,为什么在控制台中没有打印出printf("Hello World\\n") How to make it printed in console also? 也要如何在控制台上打印它?

My am working in windows-7, visual studio-2010 我正在Windows 7,Visual Studio-2010中工作

The easiest way would be to be more explicit about it, by manually printing to both stdout (the original, to get output to the console) and to your file. 最简单的方法是通过手动打印到stdout (原始文件,以将其输出到控制台)和文件上,使其更加明确。

By re-opening stdout to point at the file, you remove the connection to the console window, which is why no output appears. 通过重新打开stdout指向文件,可以删除与控制台窗口的连接,这就是为什么没有输出的原因。

You could also only use stdout , and use an external tool such as tee to duplicate the output into a file. 您也只能使用stdout ,并使用诸如tee的外部工具将输出复制到文件中。

the

freopen("myfile.txt" , "a", stdout);

will make your stdout output to the file myfile.txt This function will redirect the output from console to the file myfile.txt 会将您的stdout输出输出到文件myfile.txt此功能会将输出从控制台重定向到文件myfile.txt

Even if you use fclose(stdout); 即使您使用fclose(stdout); , this will not back the output of stdout to the console it will only close the myfile.txt ,这不会将stdout的输出返回到控制台,只会关闭myfile.txt

refer to the following link for more details Strange behaviour when redirecting stdout in C 有关更多详细信息,请参见以下链接。 在C中重定向stdout时的奇怪行为

in order to get the output in both the console and in the file, You have tokeep the stdout untouch do not reopen it with freaopen() and do not close it. 为了同时在控制台和文件中获得输出,必须保持stdout freaopen() ,不要使用freaopen()重新打开它,也不要关闭它。 and You have to print your message twice in the file and in the stdout 并且您必须在文件和标准输出中将消息打印两次

I have also faced such requirements once. 我也曾经遇到过这样的要求。

I have made one macro for such printing log. 我已经为此类打印日志制作了一个宏。 That print macro expands in one function which print that message in my log file and stdout or stderr depending upon some control variable setting. 该打印宏扩展了一个功能,该功能根据某些控制变量设置在我的日志文件和stdout或stderr中打印该消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM