C Primer Plus (8)

Source: Internet
Author: User

8.1 single character I/O: getchar () and putchar () 8.2 Buffer

When you run the preceding program On some systems, the text you entered is immediately displayed. However, in most systems, nothing happens until you press the Enter key.
The immediate echo of the input character is a non-buffered or directly input instance. It indicates that the character you typed becomes available to the waiting program immediately.
Latency ECHO is an instance of buffered input. In this case, the characters you type are collected and stored in a temporary storage area called a buffer. Press the Enter key to make the character block pair program available.

Second, if you enter an error, you can use your keyboard correction function to correct the error.

For row buffering, the buffer is cleared when a newline character is encountered. The hard disk input is a standard row buffer.

8.3 terminate keyboard input

8.3.1 file, stream, and keyboard input

A file is a storage area for storing information.
In terms of concept, the C program processes a stream instead of a file directly.
A stream is an idealized data stream. The actual input or output is mapped to this data stream.

8.3.2 end of File

One way to check the end of a file is to place a special character in the file to mark the end.
The second method is to let the operating system store the file size information.
For these two different methods, C's processing method is to let the getchar () function return a special value when it reaches the end of the file, regardless of how the operating system detects the end of the file. The name assigned to this value is EOF.
Generally, EOF is defined as-1 in stdio. H files.

 

The variable ch is changed from char type to int type. This is because the char variable can be expressed by an unsigned integer ranging from 0 to 255, but the EOF variable may have a value of-1. This value is impossible for the unsigned char variable, but it is possible for the int variable.

 

8.4 create a user-friendly interface

BUG: The program reads the n response and regards it as a rejection, then reads the line feed character and regards it as a rejection.
One solution is to use a while loop to discard the rest of the input line, including line breaks.
While (getchar ()! = '\ N ')
Continue;

Many programs use getchar () to read input characters one by one. Generally, the system uses a row to buffer the input, which means that the input content is transmitted to the program when you press the Enter key. At this time, a newline character should be transmitted during programming. Ansi c uses the buffer input as the standard.

 

Related Article

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.