C language-character I/O and Buffer

Source: Internet
Author: User

I. Simple C program I/O

Program completion: Get the characters entered from the keyboard and send them to the screen.

#include<stdio.h>;int main(void){    char ch;    while((ch=getchar())!='#')        putchar(ch);    return 0;}

 

We will think of a question: why do we have to enter a full row before explicit input. You may want to get the following results:

Mmyy nnaammee IISS rroonnnnyy yyoouunngg

Ccoomatrix puutteerr

Ii. Buffer Zone

Immediate echo of the input character is a non-buffering or direct input form, which indicates that the character you typed becomes available immediately to the waiting program. Conversely, latency ECHO is an example of buffering input. In this case, the character blocks you type are collected and stored in a temporary storage area called a buffer. Press the Enter key to enable the entered character segment to work for the program.

Buffer input is usually used in text programs. When there is a mistake in the input, you can use your keyboard to correct the error. When you press the Enter key, you can send the correct input.

In some interactive games, non-buffered input is required. For example, when you press a key in the game, you must execute a command.

There are two types of buffering:

1. Full Buffer: the buffer is cleared when it is full (the content is sent to its destination ). This type of buffer usually appears in the file input.

2. Row buffering: When a newline character is encountered, the buffer is cleared. The keyboard input is a standard row buffer, so press the Enter key to clear the buffer.

3. redirection and file

Generally, the simple C program we write uses a keyboard as the input and a screen to display the output result. We can use a redirection command and a file as the input or output.

1. input redirection: studyc <input.txt

2. Output redirection: studyc> output.txt

3. Combined redirection: studyc <input.txt> output.txt or studyc> output.txt <input.txt

4. buffer input

The following program keeps reading characters. When the input characters do not match the preset, there is a warning.

#include<stdio.h>;int main(void){    char ch;    while((ch=getchar())!='a')        printf("Warning,please Enter another char.\n");    return 0;}

Run the program and you will get the following results:

We can see that every time we input an error match, two warnings are issued. The reason is that when we press enter to submit the input, the press enter will be judged as the next input.

You can use the following method to avoid this problem:

#include<stdio.h>;int main(void){    char ch;    while((ch=getchar())!='a')    {        printf("Warning,please Enter another char.\n");        while(getchar()!='\n')            continue;    }    return 0;}

A getchar () read loop is used to receive carriage return and other redundant input.

Remember, when you use a loop to continuously read characters, you must consider the impact of line breaks as the next input. As in the code above, add a loop to the end to read the characters, the following characters can be ignored.

5. prompt the user to enter an integer

Sometimes we need users to enter a character or number, but sometimes users do not do what we want, then we need to use good reminder and processing capabilities.

#include<stdio.h>int main(void){    int input;    char ch;    while((scanf("%d",&input))!=1)    {        while((ch=getchar())!='\n')            putchar(ch);        printf(" is not an integer number.\n");        printf("Please Enter an integer value,such as 25,-178, or 3:");    }    printf("The integer number you input is %d\n",input);    return 0;}

The running result of the program is as follows:

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.