900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > Linux C语言实现输入密码显示星号-手动实现getch()

Linux C语言实现输入密码显示星号-手动实现getch()

时间:2020-11-26 06:22:26

相关推荐

Linux C语言实现输入密码显示星号-手动实现getch()

Linux C语言实现输入密码显示星号-手动实现getch()

废话不多说直接上代码

github传送门

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <termios.h>#include <unistd.h>int getch(void);void get_password(char *password);int main(){char password[20];get_password(password);printf("%s\n", password);return 0;}int getch(void){int ch;struct termios tm, tm_old;tcgetattr(STDIN_FILENO, &tm);tm_old = tm;tm.c_lflag &= ~(ICANON | ECHO);tcsetattr(STDIN_FILENO, TCSANOW, &tm);ch = getchar();tcsetattr(STDIN_FILENO, TCSANOW, &tm_old);return ch;}void get_password(char *password){int i = 0;char ch;printf("Enter password: ");while ((ch = getch()) != '\n'){if (ch == '\b'){if (i > 0){printf("\b \b");i--;}}else{password[i] = ch;printf("*");i++;}}password[i] = '\0';printf("\n");}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。