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