Stderr,stdout,a.txt Buffer Difference

Source: Internet
Author: User

#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

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.