#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <error.h>
#include <errno.h>
#include <unistd.h>
#include <strings.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
int main (int argc,char **argv)
{
Fputs ("To stdout", stdout);//Line buffer, only after the string refueling ' \ n ', will be displayed to the screen
Fputs ("To stderr", stderr);//unbuffered, direct display to screen
FILE *FP = fopen ("A.txt", "w");//full buffer, generally 4k,windows for 1k.
if (fp = = NULL)
{
fprintf (stderr, "fopen ()%s failed:%s", "A.txt", Strerror (errno));
Exit (0);
}
Fputs ("To A.txt", FP);
Pause ();
Fflush (FP);//forced flush full buffering.
return 0;
}
----------------------------------------------
Several refreshes note:
About Standard IO buffers
================================
1, non-buffered (standard error stream stderr)
1.1 Once there is data in the buffer, refresh immediately.
2, full buffer (normal file)
2.1 Once the buffer fills up, refresh immediately (do exercise: detect the full buffer size)
2.2 Call Fflush () to force the flush.
2.3 When the program exits normally, it refreshes immediately.
2.3.1 return in the main function
2.3.2 Call Exit ()/_exit ()/_exit () Anywhere
2.3.3 Call Pthread_exit () in the last thread
2.4 Call Fclose () when the file is closed, refresh immediately.
2.5 When you call Setvbuf ()/setbuf () To change the buffer type, refresh immediately.
3, row buffer (standard output stream stdout)
3.1 Once the buffer fills up, refresh immediately (do exercise: detect the full buffer size)
3.2 Call Fflush () to force the flush.
3.3 When the program exits normally, it refreshes immediately.
3.3.1 return in the main function
3.3.2 Call Exit ()/_exit ()/_exit () Anywhere
3.3.3 Call Pthread_exit () in the last thread
3.4 Call Fclose () when the file is closed, refresh immediately.
3.5 When you call Setvbuf ()/setbuf () To change the buffer type, refresh immediately.
3.6 once encountered ' \ n '
Stderr,stdout,a.txt Buffer Difference