Talk C Chestnut Bar (170th: C Language Instance--turn off the Echo function in terminal one)

Source: Internet
Author: User

Ladies and gentlemen, crossing, the last time we talked about the example of a built-in macro, this time we say the example is: turn off the echo function in the terminal . Gossip Hugh, words return to the positive. Let's talk C chestnuts together!

Crossing, what we're going to do today is to turn off the Echo feature in the terminal, first to describe what is the echo feature. When we enter characters in the terminal, the terminal will immediately display the characters we entered, which is the echo function. I think this kind of operation, everyone has been commonplace, just don't know it also has a professional name: Echo.

The echo gives us a lot of convenience, for example, we can see what we have entered into the terminal in time, and can be modified when the input content is wrong. Then there is crossing said: Really good! Very powerful! It's perfect! Haha, do not say so absolutely, sometimes we do not want to use echo, such as when we enter the password, we do not want to use echo, so that other people see their own password. In fact, in the Linux system also has such a situation, for example, to execute the sudo command to enter the root password, this time the terminal turned off the Echo function, then the terminal is how to turn off the echo function? This is the theme of our day.

There is a command called Stty, can display or modify the terminal's settings, we can use this command to turn off the terminal echo function, the specific command is:

  • Stty-echo//Turn off the echo function of the terminal
  • Stty Echo//Open Terminal's echo function

It's not our style to say it, but let's take a concrete example to illustrate:

talk8$ stty -echo    //输入命令,这时有回显功能,因为我们可以看到输入的命令                     //执行命令后,关闭了回显功能,我们在终端中输入命令date,然后执行该命令2016070220:35:01//执行date命令后的结果                     //输入命令stty echo,这时回显功能被关闭,看不到输入的命令talk8$ talk8$        //执行打开回显命令后的结果

After understanding the command, let's look at how to use this feature in a C program. Do you remember the function we introduced earlier Zhanghuizhong system() ? We can use this function to execute terminal commands in a C program. Next we demonstrate the use of the function using specific code:

    system("stty -echo"//close the ECHO functoin    if(NULL != fgets(buf,BUFSIZ,stdin))        printf("input is : %s ",buf);    system("stty echo"//open the ECHO function

Crossing, the above is the core code, the complete code put in my resources, you can click here to download the use. Because the echo function is closed in the terminal, we can not see the content on the screen, only through the text expression may not be able to achieve a good effect, so we recommend that you download the program, and compile and run it yourself, actually realize the operation after turning off the echo function.

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

functionisisfunctionis closed ---please input some charaters  is : world          //关闭回显功能后看不到输入的内容,该内容是printf()函数输出的

Crossing, for example, "Let's talk about this." I want to know what the following example, and listen to tell.

Talk C Chestnut Bar (170th: C Language Instance--turn off the Echo function in terminal one)

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.