【发布时间】:2011-06-26 08:14:05
【问题描述】:
【问题讨论】:
-
这个操作系统会很方便
-
Enter Password in C 和其他几个的可能副本。
标签: c
【问题讨论】:
标签: c
您需要将控制台设置为“无回显”模式。这取决于您的特定操作系统。这是为 linux 做的一个例子:http://www.cplusplus.com/forum/beginner/1988/page4.html#msg14522
【讨论】:
请参阅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;
}
【讨论】: