C + + stderr/stdout Redirect to file

Source: Internet
Author: User

Typically, stderr and stdout are used to display output content to the screen, but sometimes we need to write this information to the specified file for easy access at any time. The simplest way to achieve this is to put the output of the Stderr/stdout redirectto the file.
stderr/stdout Redirect to fileThis is illustrated by the stderr code.
#include <stdio.h> #include <stdlib.h>int main (void) {FILE *stream = freopen ("Freopen.out", "W", stderr); if ( stream = = NULL) fprintf (stdout, "error on freopen\n"), else{fprintf (stdout, "successfully reassigned\n"); Fflush (stdo UT); fprintf (Stream, "This would go to the file ' freopen.out ' \ n"); fprintf (stderr, "Also you can do it like this!\n"); FC Lose (stream);} Windwos Read the file Freopen.outsystem ("type Freopen.out"); GetChar (); return 0;}
Execution results are as follows,

the difference between stderr and stdout stdout (standard output), the output mode is row buffering. The output character is stored first in the buffer, and then the actual I/O operation is performed when the ENTER key is pressed. stderr (standard error)is not buffered, which allows the error message to be displayed directly as soon as possible.
description of the buffer:
Full buffer I/O operations do not occur until the buffer is filled 1. Buffer Full 2. Brush out data (fflush) 3. Close File (fclose)
Row buffers The actual I/O operation is usually performed only when a newline character is encountered, but the buffer full is also enforced 1. Line breaks encountered 2. Buffer Full 3. Brush out data (fflush) 4. Close file (fclose)
No buffering Do not cache, direct I/O operation Direct output

However, as far as buffering is concerned, there is no absolute difference between stdout and stderr because the buffer type can be set. Use the setvbuf () or setbuf () function here.
#include <stdio.h>  #include <stdlib.h>    int main (void)  {      char buf[512] = {0};setbuf ( stderr, buf), fprintf (stderr, "It is Error 1\n");p rintf ("Echo 1\n"), fprintf (stderr, "It is Error 2\n");p rintf ("Echo 2\n"); fprintf (stderr, "It is Error 3\n"); Fflush (stderr); GetChar (); return 0;}  
The results of the operation are as follows:This allows us to define the buffer size. The default size of the buffer is defined by the macro bufsiz in the stdio.h header file, which is 512 bytes. Also, check some information that the minimum can not be less than 256 bytes, but the test example does not have this problem (not yet).


setvbuf () and setbuf ()The setvbuf () function is prototyped as follows:
int setvbuf (FILE * stream, char * buffer, int mode, size_t size);
Setbuf () can be used as a call to Setvbuf (stream, buf, buf? _IOFBF: _IONBF, BUFSIZE);
Where mode is the type of the declaration buffer, as follows:
_IOFBF
_IOLBF row buffer
_IONBF No buffering
Size is the buffer size, in bytes.
/* SETVBUF Example */#include <stdio.h>int main () {  FILE *pfile=fopen ("MyFile.txt", "w");  Setvbuf (PFile, NULL, _IOFBF, 1024x768);  File operations here  fclose (pFile);  return 0;}


Reference: http://blog.csdn.net/mycwq/article/details/46554805

C + + stderr/stdout Redirect to file

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.