c Buffers and files

Source: Internet
Author: User
Tags stdin
A buffer is also known as a cache, which is part of the memory space. In other words, a certain amount of storage space is reserved in the memory space, which is used to buffer the input or output data, which is called the buffer space.

The buffer is divided into input buffers and output buffers depending on whether it corresponds to an input device or an output device. why to introduce buffersFor example, we take the information from the disk, we first put the read data in the buffer, the computer then take the data directly from the buffer, and so on after the buffer data is fetched to the disk to read, so that can reduce the number of disk read and write, plus the computer to the buffer operation is much faster than the operation of the disk, Therefore, the application of buffer can greatly improve the speed of computer operation.

For example, we use the printer to print the document, because the printer printing speed is relatively slow, we first output the document to the corresponding buffer of the printer, the printer and then gradually print themselves, then our CPU can handle other things.

Now you basically understand that the buffer is a block of memory that is used between the input and the CPU to cache the data. It enables low-speed input and high-speed CPUs to work in a coordinated manner, avoiding low-speed input consuming the CPU and freeing the CPU to work efficiently. type of bufferThere are three types of buffers: full-buffered, row-buffered, and unbuffered.

1) Full buffer
In this case, the actual I/O operation is not performed until the standard I/O cache is filled. A typical representation of a full buffer is the read and write of a disk file.

2) Row buffer
In this case, the real I/O operation is performed when a newline character is encountered in the input and output. At this point, we enter the word Mr. Foo stored in the buffer, and so on when the return hit enter the actual I/O operation. Typical representations are standard input (stdin) and standard output (stdout).

3) without buffering
That is, no buffering, standard error stderr is a typical representative, which makes the error message can be displayed directly as soon as possible.

ANSI C (C89) requires that the cache have the following characteristics: They are fully cached when and only if the standard input and standard output do not involve interactive devices. Standard errors are never fully cached.
However, this does not tell us if the standard inputs and outputs involve interaction devices, they are not cached or row cached, and whether the standard output is cached or row-cached.

Most systems use the following types of caches by default: standard errors are not cached. If it is a stream that involves a terminal device, then they are row-cached; otherwise they are fully cached.
We often use standard input and output streams, and ANSI C has no mandatory rules for stdin, stdout, and stderr cache features, so that different systems may have different caching characteristics for stdin, stdout, and stderr. The main caching features are: StdIn and stdout are row caches, and stderr is cache-free.
size of the bufferIf we do not set our own buffer, the system will default to the standard input output set a buffer, the size of the buffer is usually 512 bytes in size.

The buffer size is defined by the macro bufsiz in the stdio.h header file, and if you want to see its size, include the header file and output its value directly:
printf ("%d", bufsiz); The size of the buffer can be changed, or the file can be associated to a custom buffer, and the setvbuf () and setbuf () functions can be viewed in detail.
refresh of buffer (empty)The following conditions cause a buffer refresh: When the buffer is full, the row buffer encounters a carriage return, the file is closed, and the buffer is flushed with a specific function. This article will use the concept of the C language buffer, if you do not understand the buffer, please review: C language buffer (cache)

A comparison of three functions Echo Echo
-- buffers header File
GetChar () Has buffer Stdio.h There is a return display
Getch () No buffer Conio.h No return display
Getche () No buffer Conio.h There is a return display
GetChar () functionFirst look at GetChar (), whose prototype is:
int GetChar (void);

When the program calls the GetChar () function, the program waits for the user to press the key, and the character entered by the user is stored in the keyboard buffer until the user presses ENTER (the carriage return character is also placed in the buffer). The GetChar () function starts to read one character at a time from the keyboard buffer when the user enters enter. That is, subsequent getchar () function calls do not wait for the user to press the key, but directly read the characters in the buffer until the characters in the buffer are read, and then wait for the user to press the key again.

In layman's terms, when the program calls the GetChar () function, the program waits for the user to press the key and then returns when the user presses ENTER. The characters that are pressed during the period are stored in the buffer, and the first character returns a value as a function. Continue calling the GetChar () function, instead of waiting for the user to press the key, return the 2nd character you just entered, continue the call, return the 3rd character, and wait for the user to press the key until the character in the buffer has been read.

One example below will give you a deep understanding of:
#include <stdio.h> int main () {char C;//First call GetChar () function//program execution, you can enter a string of characters and press ENTER, press ENTER after the function returns C=getchar (); Displays the return value of the GetChar () function printf ("%c\n", c); Pause System ("pause"); while ((C=getchar ()) = ' \ n ') {printf ("%c", c);}//Pause system ("pause"); return 0; This small piece of code is simple and has comments inside the code.

The execution of the GetChar () function is the use of a row buffer. The first call to the GetChar () function causes the program consumer (user) to enter a line of characters and return until the ENTER function is pressed. The character and carriage return entered by the user at this time are stored in the row buffer.

Calling the GetChar () function again will progressively output the contents of the row buffer.

Please look at the following example:
#include <stdio.h> int main () {char ch1; char ch2; ch1 = GetChar (); CH2 = GetChar (); printf ("%d%d", CH1, CH2); ret Urn 0; The intent of the program is simply to read the two characters from the keyboard and print out the ASCII values of the two characters. However, the execution of the program will find a problem: When the keyboard input a character, printed out the results, there is no input of the second word Fu Chengxu will end. For example, the user enters the character ' a ', the print result is 97, 10. What is this for?

The GetChar () function reads data from the input stream buffer, not from the keyboard (terminal) buffer. When the read encounters a carriage return (\ n) at the end, the ' \ n ' will be read into the input stream buffer, so the first time the input is taken away characters will leave the character \ n, so that the second time GetChar () directly from the buffer to take away, apparently read successfully, so no longer read from the terminal. Actually, the 10 here happens to be a carriage return character. This is why the program only executes one input operation at the end.
Getch () and Getche () functionsIn the TC2.0 era, C programmers always like to add Getch () at the end of the program to achieve the effect that the program runs out of pause. If you do not, you will quit the program immediately after compiling and running in the TC2.0 environment, and you won't see the results ctrl+f9. At this point, if you want to see the results, it is necessary to press ALT+F5 back to the DOS environment, very troublesome. And if at the end of the program add a line Getch (), the statement, you can save back DOS see the result of this step, because the program runs out and does not exit, but at the end of the program to stop the screen, press any key to quit the program.

In fact, the Getch () function is to receive a character from the keyboard without echoing. That is, you press a key after it does not display what you press on the screen, and continue to run the following code, so in C language can be used to achieve "press any key to continue" effect, that is, the program encountered Getch (), the statement will stop, and so you press any key, It receives the character key before continuing to execute the code that follows.

Getche () and Getch () are similar, it also need to introduce the header file conio.h, the difference between them is: Getch () No Echo, Getche () has echo. Take a look at the following example:

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.