Excerpted from http://blog.chinaunix.net/uid-26565142-id-3051729.html
1, the file IO function , in Unix, has the following 5: Open,read,write,lseek,close. Known as IO without cache (unbuffered I/O). Without caching, each read,write is called by a system call in the kernel.
2, standard IO library , described by ANSI C standard. The standard IO Library handles many details. such as cache allocation, to optimize the length of the execution io . Standard IO provides three types of caching.
(1) Full cache: The actual IO operation is not performed until the standard IO cache is filled.
(2) Row cache: The standard IO library performs IO operations when new line characters are encountered in input or output.
(3) without cache: stderr.
3, What are the benefits of caching?
The program can write data to the output stream than do from Beijing to transport the roast duck to Shanghai. If there is no buffer, then each execution of the write (int b) method, only a roast duck from Beijing to Shanghai, if the 10,000 roast duck, it will be transported 10,000 times, so the efficiency is obviously very low. In order to reduce the number of shipments, you can first put a batch of roast duck into a container, so that can be shipped in batches of roast duck, this container is the buffer zone.
By default, the container will be shipped to Shanghai (full cache) only when it is filled, and the flush method means that the shipping operation is carried out regardless of whether the container is filled or not.
To ensure that the input and output streams are closed in a timely manner, it is best to place the operation of the closed stream in the finally code block.
The difference between the file IO function and the standard IO library