【问题标题】:How can I get characters for a password from the console but echo them as asterisks [duplicate]如何从控制台获取密码字符但将它们作为星号回显[重复]
【发布时间】:2017-11-26 14:59:52
【问题描述】:

我想知道如何从控制台获取字符

scanf, getch()

或者别的什么,在用户输入我希望屏幕显示的时候:

******

每个字符。

【问题讨论】:

  • 你使用的是什么操作系统?

标签: c input


【解决方案1】:

如果你在 Windows 上,我认为你可以使用下面给出的东西 -

#include <stdio.h>
#include <conio.h>
int main(){
    char str[8];
    int i=0;
    printf("Enter the password :\n");
    while (i< 8){
        str[i]=getch();
        printf("*");
        i++;
    }
    str[i]='\0';
    return 0;
}

getch() 函数在类 Unix 系统上并不那么容易使用。 (cursesncurses 库提供了一个函数getch(),但您必须调用函数才能正确设置终端,然后将其重置回已知状态。)

【讨论】:

    猜你喜欢
    • 2012-06-15
    • 2017-11-06
    • 1970-01-01
    • 2015-01-18
    • 1970-01-01
    • 2016-12-19
    • 2018-10-05
    • 1970-01-01
    • 2014-01-22
    相关资源
    最近更新 更多