我想知道如何使用控制台获取字符
scanf, getch()
或者其他东西,当用户输入时我希望屏幕显示:
******
对于每个角色。
最佳答案
如果您使用的是 Windows,我认为您可以使用如下所示的内容 -
#include <stdio.h>
#include <conio.h>
int main(){
char str[8];
int i=0;
printf("Enter the password :\n");
while (i< 8){
str[i]=getch();
printf("*");
i++;
}
str[i]='\0';
return 0;
}
getch()
函数在类 Unix 系统上不太容易使用。 (curses
或 ncurses
库提供了函数 getch()
,但您必须调用函数来正确设置终端,然后重置它返回到已知状态。)
关于c - 如何从控制台获取密码字符,但将它们显示为星号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47497457/