可能重复:
Enter Password in C
要求用户输入密码而不在控制台上显示输入字符?
答案 0 :(得分:2)
您需要将控制台设置为“无回显”模式。这取决于您的特定操作系统。以下是为linux执行此操作的示例:http://www.cplusplus.com/forum/beginner/1988/page4.html#msg14522
答案 1 :(得分:0)
请参阅GNU libc documenation for getpass,它提供了一个如何实现此功能的简单示例:
#include <termios.h>
#include <stdio.h>
ssize_t
my_getpass (char **lineptr, size_t *n, FILE *stream)
{
struct termios old, new;
int nread;
/* Turn echoing off and fail if we can't. */
if (tcgetattr (fileno (stream), &old) != 0)
return -1;
new = old;
new.c_lflag &= ~ECHO;
if (tcsetattr (fileno (stream), TCSAFLUSH, &new) != 0)
return -1;
/* Read the password. */
nread = getline (lineptr, n, stream);
/* Restore terminal. */
(void) tcsetattr (fileno (stream), TCSAFLUSH, &old);
return nread;
}