可能重複:
Hide password input on terminal如何顯示*爲每個輸入字符在C密碼 - 使字符無形
我要實現這一點:
$Insert Pass:
User types: a (a immediately disappears & '*' takes its position on the shell)
On the Shell : a
Intermediate O/P: *
User types: b (b immediately disappears & '*' takes its position on the shell)
On the Shell : *b
Intermediate O/P: **
User types: c (c immediately disappears & '*' takes its position on the shell)
On the Shell : **c
Final O/P : ***
我試過以下方法:
#include <stdio.h>
#include <string.h>
#define SIZE 20
int main()
{
char array[SIZE];
int counter = 0;
memset(array,'0',SIZE);
while ((array[counter]!='\n')&&(counter<=SIZE-2))
{
array[counter++] = getchar();
printf("\b\b");
printf ("*");
}
printf("\nPassword: %s\n", array);
return 0;
}
但我無法達到預期的輸出。此代碼無法使用戶鍵入的字符不可見&立即顯示'*'。
有人可以請指導我這一點。
謝謝。
最好的問候, 桑迪普·辛格
我認爲這已經在這裏得到解決 - http://stackoverflow.com/questions/6856635/hide-password-input-on-terminal –
@RudolfsBundulis是的,他們確實討論這一點。要走的路將是這樣的答案:http://stackoverflow.com/a/6869218/694576 – alk