C ++ stderr/stdout redirects to the file, stderrstdout
Generally, stderr and stdout are used to output content to the screen. However, sometimes we need to write this information to the specified file for convenient access at any time. The simplest implementation method is to redirect stderr/stdout output to a file.
Stderr/stdout redirection to a fileThe stderr code is described here.
# 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 (stdout); fprintf (stream, "This will go to the file 'freopen. out' \ n "); fprintf (stderr," Also you can do it like this! \ N "); fclose (stream);} // read the file freopen. outsystem (" type freopen. out "); getchar (); return 0;} In windwos ;}
The execution result is as follows,
Difference between stderr and stdout
Stdout (standard output)The output mode is row buffering. The output characters are first stored in the buffer zone. The actual I/O operation is performed only when the Enter key is pressed.
Stderr (standard error), Is not buffered, so that the error information can be displayed directly as soon as possible.
Buffer description:
Type |
Description |
Output status |
Full Buffer |
I/O operations are performed only after the buffer is filled up. |
1. buffer full 2. Flush data (fflush) 3. close the file (fclose) |
Row Buffer |
The actual I/O operation is usually performed only when a line break is encountered, but the execution is also enforced when the buffer is full. |
1. Encountering line breaks 2. buffer full 3. Flushing data (fflush) 4. Closing the file (fclose) |
No Buffer |
Directly perform I/O operations without caching |
Direct output |
However, there is no absolute difference between stdout and stderr as the buffer type can be set. The setvbuf () or setbuf () function is used here.
#include <stdio.h> #include <stdlib.h> int main( void ) { char buf[512] = {0};setbuf(stderr, buf);fprintf(stderr, "It is error 1\n");printf("echo 1\n");fprintf(stderr, "It is error 2\n");printf("echo 2\n");fprintf(stderr, "It is error 3\n");fflush(stderr);getchar();return 0;}
The running result is as follows: we can define the buffer size. The default buffer size is defined by the macro bufsiz in the stdio. h header file, which is 512 bytes. In addition, I can refer to some materials to say that the minimum length should not be less than 256 bytes, but this problem is not found in the test example (I am not investigating it for the time being ).
Setvbuf () and setbuf ()The following is a prototype of the setvbuf () function:
int setvbuf ( FILE * stream, char * buffer, int mode, size_t size );
Setbuf () can be called as setvbuf (stream, buf, buf? _ IOFBF: _ IONBF, BUFSIZE );
Among them, mode is the declared buffer type, the following:
_IOFBF |
Full Buffer |
_IOLBF |
Row Buffer |
_IONBF |
No Buffer |
Size is the buffer size, in bytes.
/* setvbuf example */#include <stdio.h>int main (){ FILE *pFile=fopen ("myfile.txt","w"); setvbuf ( pFile , NULL , _IOFBF , 1024 ); // File operations here fclose (pFile); return 0;}
Reference: http://blog.csdn.net/mycwq/article/details/46554805