【问题标题】:Ask user to enter Password, without showing the input characters on console? [duplicate]要求用户输入密码,而不在控制台上显示输入字符? [复制]
【发布时间】:2011-06-26 08:14:05
【问题描述】:

可能重复:
Enter Password in C

要求用户输入密码而不在控制台上显示输入字符?

【问题讨论】:

标签: c


【解决方案1】:

您需要将控制台设置为“无回显”模式。这取决于您的特定操作系统。这是为 linux 做的一个例子:http://www.cplusplus.com/forum/beginner/1988/page4.html#msg14522

【讨论】:

  • @Gaurav:@Andrey 发布的链接有 C 代码。
【解决方案2】:

请参阅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;
 }

【讨论】:

    猜你喜欢
    • 2010-12-09
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 2016-12-09
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多