Linux system resolved Getch () input value does not show back

Source: Internet
Author: User

Under the Linux system development C program will encounter the system does not support Conio.h header file, cannot use Getch () do not echo function. The following shows how to construct a function to implement numeric input without echoing.

1#include <stdio.h>2 3#include <termios.h>4 5#include <unistd.h>6 7#include <errno.h>8 9 #defineEchoflags (ECHO | Echoe | Echok | ECHONL)Ten  One //function Set_disp_mode to control whether the input echo function is turned on A  - //if option is 0, the echo is turned off, and 1 turns on the Echo -  the intSet_disp_mode (intFdintoption) -  - {   -  +    interr;  -  +    structTermios term;  A  at    if(Tcgetattr (fd,&term) ==-1){   -  -Perror ("cannot get the attribution of the terminal");  -  -      return 1;  -  in    }   -  to    if(option) +  -term.c_lflag|=Echoflags;  the  *    Else   $ Panax NotoginsengTerm.c_lflag &=~Echoflags;  -  theErr=tcsetattr (fd,tcsaflush,&term );  +  A    if(err==-1&& err==eintr) {   the  +Perror ("cannot set the attribution of the terminal");  -  $         return 1;  $  -    }   -  the    return 0;  - Wuyi }   the  - //The function getpasswd is used to get the password entered by the user and stores it in the specified character array Wu  - intGETPASSWD (Char* passwd,intsize) About  $ {   -  -    intC;  -  A    intn =0;  +  the      -  $printf"Please Input password:");  the  the      the  the     Do{   -  inC=GetChar ();  the  the       if(c! ='\ n'|c!='\ r'){   About  thepasswd[n++] =C;  the  the       }   +  -} while(c! ='\ n'&& C! ='\ r'&& N < (Size-1));  the BayiPasswd[n] =' /';  the  the    returnN;  -  - }   the  the intMain () the  the {   -  the    Char*p,passwd[ -],name[ -];  the  theprintf"Please Input name:"); 94  thescanf"%s", name);  the  theGetChar ();//turn the carriage return masking out .98  About    //first turn off output echo so that the input character information is not displayed when you enter a password - 101Set_disp_mode (Stdin_fileno,0); 102 103    //Call the GETPASSWD function to get the password entered by the user104  theGETPASSWD (passwd,sizeof(passwd)); 106 107p=passwd; 108 109     while(*p!='\ n')   the 111p++;  the 113*p=' /';  the  theprintf"\nyour name is:%s", name);  the 117printf"\nyour passwd is:%s\n", passwd); 118 119printf"Press any key continue ... \ n");  - 121Set_disp_mode (Stdin_fileno,1); 122 123 GetChar (); 124  the    return 0; 126 127 }   - 129   the 131   the 133  

Operation Result:

Description: Linux under the C programming encounter to enter the password problem, you can enter the password always can not let people see it, originally wanted to use Getch () to solve the problem of input password no echo, and unexpectedly linux-c not support getch (), I also did not find function similar function instead, The above example achieves the desired effect.

Linux system resolved Getch () input value does not show back

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.