2
當用C語言提問時,有什麼方法可以隱藏用戶輸入嗎? 例如:隱藏用戶輸入,只允許某些字符
char *str = malloc(sizeof(char *));
printf("Enter something: ");
scanf("%s", str);getchar();
printf("\nYou entered: %s", str);
// This program would show you what you were writing something as you wrote it.
// Is there any way to stop that?
另一件事,是你怎麼能只允許某些字符? 例如:
char c;
printf("Yes or No? (y/n): ");
scanf("%c", &c);getchar();
printf("\nYou entered: %c", c);
// No matter what the user inputs, it will show up, can you restrict that only
// showing up if y or n are entered?
側面說明:'字符*海峽=的malloc(的sizeof(字符*));'似乎是錯誤的。 scanf是不安全的讀取C字符串 – 2012-03-16 04:35:20
可能重複[從std :: cin讀取密碼](http://stackoverflow.com/questions/1413445/read-a-password-from-stdcin)(即使OP是沒有詢問密碼輸入,鏈接線程中接受的帖子顯示如何禁用/啓用'終端回聲') – 2012-03-16 04:35:32
忘了提及環境,這是一種posix兼容shell,win console或什麼? 你的終端處理輸入緩衝區和afaik沒有便攜的方式來做到這一點。 – AoeAoe 2012-03-16 04:36:05