STDOUT (standard output stream) and stderr (standard input stream) redirection issues

Source: Internet
Author: User

 I want to write both the message and the error message list to the same file.

1 /*2 * * Set the stream so that both the output and the error flow point to the specified file3 */4 if(OUTPUT1 = Freopen (PATH,"a", stdout)) = =NULL)5 exit (exit_failure);6     7 if(Output2 = Freopen (PATH,"a", stderr)) = =NULL)8Exit (Exit_failure);

At first, I wrote this.

" Error List: \ n " , OUTPUT1);  for 1 ; i + + )       {= i       ; " ERROR "  );}

As a result, the contents of the file are in the error message list earlier, prompting the message at the end.

I Baidu, found that the stdout flow buffer is a full buffer, stderr is not buffered, the full buffer is the buffer is full after the write, so I used setvbuf change the stdout buffer (also declare a large enough array buffer), change to not buffer, That is, add a sentence before the code.

SETVBUF (output1, buffer, _IONBF, bufsiz);

In this way, the problem is solved.

Another way is to add a sentence of fflush (OUTPUT1) after fputs; This means that the contents of the buffer are written immediately.

Finally, the results are posted, hehe

STDOUT (standard output stream) and stderr (standard input stream) redirection issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.