有些系统或软件输入密码时看不见我们所输入的字符,这里写一下这个简单代码:
运行后,只要不输入‘x’,就别想继续运行,而输入的字符也全部都看不见!
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
#include <termios.h>
int main()
{
struct termios old, new;
tcgetattr(0, &old); // 获取终端属性
new = old;
new.c_lflag &= ~(ECHO | ICANON);
// 不使用标准的输出,不显示字符。
tcsetattr(0, TCSANOW, &new);
// 设置终端新的属性
while((fgetc(stdin)) != 'x'){
;
}
tcsetattr(0, TCSANOW, &old);
// 恢复终端的属性
return 0;
}