In linux, if the input value of getch () is not displayed, linuxgetch

Source: Internet
Author: User

In linux, if the input value of getch () is not displayed, linuxgetch

In linux, C programs are developed, but the system does not support the conio. h header file. getch () cannot be used to not echo the function. The following example shows how to build a function to achieve non-echo of numeric input.

1 # include <stdio. h> 2 3 # include <termios. h> 4 5 # include <unistd. h> 6 7 # include <errno. h> 8 9 # define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL) 10 11 // The set_disp_mode function is used to control whether the input ECHO function is enabled 12 13 // if option is 0, if this parameter is set to 1, the echo 14 15 int set_disp_mode (int fd, int option) 16 17 {18 19 int err; 20 21 struct termios term is enabled; 22 23 if (tcgetattr (fd, & term) =-1) {24 25 perror ("Cannot get the attribution of the termina L "); 26 27 return 1; 28 29} 30 31 if (option) 32 33 term. c_lflag | = ECHOFLAGS; 34 35 else 36 37 term. c_lflag & = ~ ECHOFLAGS; 38 39 err = tcsetattr (fd, TCSAFLUSH, & term); 40 41 if (err =-1 & err = EINTR) {42 43 perror ("Cannot set the attribution of the terminal"); 44 45 return 1; 46 47} 48 49 return 0; 50 51} 52 53 // function getpasswd is used to obtain the password entered by the user and store it in the specified character array 54 55 int getpasswd (char * passwd, int size) 56 57 {58 59 int c; 60 61 int n = 0; 62 63 64 65 printf ("Please Input password :"); 66 67 68 69 do {70 71 c = getchar (); 72 73 if (c! = '\ N' | c! = '\ R') {74 75 passwd [n ++] = c; 76 77} 78 79} while (c! = '\ N' & c! = '\ R' & n <(size-1); 80 81 passwd [n] =' \ 0'; 82 83 return n; 84 85} 86 87 int main () 88 89 {90 91 char * p, passwd [20], name [20]; 92 93 printf ("Please Input name :"); 94 95 scanf ("% s", name); 96 97 getchar (); // block the carriage return to 98 99 // disable the output echo first, in this way, the entered character information 100 101 set_disp_mode (STDIN_FILENO, 0) is not displayed. 102 103 // call the getpasswd function to obtain the user-Entered password 104 105 getpasswd (passwd, sizeof (passwd); 106 107 p = passwd; 108 109 whi Le (* p! = '\ N') 110 111 p ++; 112 113 * p =' \ 0'; 114 115 printf ("\ nYour name is: % s", name ); 116 117 printf ("\ nYour passwd is: % s \ n", passwd); 118 119 printf ("Press any key continue... \ n "); 120 121 set_disp_mode (STDIN_FILENO, 1); 122 123 getchar (); 124 125 return 0; 126 127 128} 129 130 131 132 133

Running result:

Note: in Linux, when C Programming encounters the problem of entering a password, the password cannot be seen at the time of input. I originally wanted to use getch () but getch () is not supported in Linux-C, and I have not found a function like function. The above example has achieved the expected results.

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.