自测之Lesson7:设备文件操作
题目:请编写一个输入密码(不回显)的程序,要求通过设置终端来完成。
完成代码:
#include <stdio.h>
#include <unistd.h>
#include <termio.h>
int main()
{
struct termio new, old;
ioctl(STDIN_FILENO, TCGETA, &old);
new = old;
new.c_lflag &= (~ECHO);
ioctl(STDIN_FILENO, TCSETA, &new);
printf("Please input password:");
char szPass[20];
scanf("%s", szPass);
printf("\nYour password is %s\n", szPass);
ioctl(STDIN_FILENO, TCSETA, &old);
return 0;
}

浙公网安备 33010602011771号