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