C语言 如何不回显密码

2024-11-20 02:24:39
推荐回答(1个)
回答1:

#include
#include
#include
char Getch()
{
char ch;
struct termios oldt, newt;

tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ECHO|ICANON);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return ch;
}
void getpasswd(char *p)
{
int i=0;
printf("password:");

while((*(p+i)=Getch())!='\n')
{
putchar('*');
i++;
}
*(p+i) = '\0';
}
int main()
{
char a[10];
getpasswd(a);
printf("%s",a);
}