A talk C Chestnut bar (174th time: C Language Instance--closing the buffering function in the Terminal II)

Source: Internet
Author: User

Ladies and gentlemen, crossing, the last time we were talking about the example of closing the buffer function in the terminal, let's go on to this example. Gossip Hugh, words return to the positive. Let's talk C chestnuts together!

Crossing, we described in the previous section how to turn off buffering in the terminal, which was achieved with the help of the system command Stty, which we will cover in another way to turn off the Echo function in the terminal. The method is to operate the terminal through the terminal's Termios programming interface. As for the introduction and programming interface of the Termios interface, we introduced it in 171, so we'll go directly to how to turn off the terminal buffering function via the Termios programming interface.

The following is a detailed procedure, please refer to:

    • 1. Use the function to tcgetattr() obtain the configuration information in the terminal, and to back up the information in order to restore the original configuration of the terminal used;
    • 2. Modify the terminal configuration information obtained in step 1, we modify the buffer function related configuration;
    • 3. Combine the modified terminal configuration information in step 2, and then use the function to tcsetattr() modify the configuration information in the terminal;
    • 4. Using the backup terminal configuration information, reply to the initial configuration of the terminal, the function will also be used tcsetattr() ;

Crossing, does this step look like a déjà vu? Haha, in fact, this step and turn off the Echo function is very similar to the steps, only a little different, that is, the content of the changes in step 2 is different.

Here is the specific code, please refer to:

    //Source codeTcgetattr (fd,&init_setting);//get the setting of terminalchange_setting = init_setting;//backup the setting of terminalChange_setting.c_lflag &= ~icanon;//close buffer function by change bit of Icanon    printf("---buffer function is closed---\ n");printf("Please input a charater \ n"); res = tcsetattr (fd,tcsaflush,&change_setting);//change the setting of terminalch = getchar ();if(0! = ch) {tcsetattr (fd,tcsanow,&init_setting);//revert the setting of terminal        printf("\ninput is:%c \ n", ch);//show \ Firstly}

There is a file descriptor FD in the code, and we can get the file descriptor of the stream from the stream through the Fileno () function. In addition, we used the method in the previous chapter to modify the terminal configuration information: The bit operation "and" was used to remove the Icanon value from the terminal configuration information. In addition, we used two ways of modifying the terminal configuration information: T CSAFLUSH和TCSANOW . Both of these modifications have been introduced in the previous chapter and are not described in detail here.

Crossing, the above is the core code, the complete code put in my resources, you can click here to download the use.

The following is the results of the operation of the program, please refer to:

--- buffer function is open ---aa                      //缓冲功能处于打开状态,这时需要按下回车键才能从终端中获取字符a--- buffer function is closed ---aa                      //缓冲功能处于关闭状态,这时不需要按下回车键就能从终端中获取字符a

Crossing, for example, let's talk about the buffering function in the terminal. I want to know what the following example, and listen to tell.

A talk C Chestnut bar (174th time: C Language Instance--closing the buffering function in the Terminal II)

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.