开发者

Invisible input for passwords? [duplicate]

This question already has answers here: Closed 12 years ago. 开发者_如何学C

Possible Duplicate:

Read a password from std::cin

I want to cin>>input but when they input something i don't want it to be visible to them. Like when you use passwd in unix to change your password where it doesn't show what you typed. i hope it is clear what i am asking. Thank you in advance.


From C++ FAQ Lite

This is not a standard C++ feature — C++ doesn't even require your system to have a keyboard or a screen. That means every operating system and vendor does it somewhat differently. Please read the documentation that came with your compiler for details on your particular installation.


cin isn't the way to do this, since the OS (usually) echoes standard input. What you need to do is handle the key events at the OS-level.


There is getpass(3) for Unix-like systems, which will do what you want.

However,

  1. It's not portable (Unix systems only).
  2. It's deprecated ("Present in SUSv2, but marked LEGACY. Removed in POSIX.1-2001.").

There is no way to do it with standard C or C++.


If what you want is something like what you do when you enter the password to login to a linux box, that behavior cannot be achieved in C or C++. You will have to use a platform specific library to do that. More specifically, console input in C and C++ is always echoed to the console.


Have it detect key presses. Think of it this way: When a user presses the 'A' key in a video game, the program runs code; don't think of it as receiving input. Think of it as detecting keystrokes and adding each keystroke to some data structure (e.g. an array).

I am familiar with the functionality in UNIX terminal that you are talking about and it doesn't allow you to backspace or anything. If you want that functionality, just do what I said above.

If you want to be able to backspace, although the user won't be able to see what they backspace, you can write code that removes the last element of the array when the user clicks backspace.

Also, everyone else is correct when they say that you will need to program this specific to the system you're running it on.

Lastly, this might not be secure if you are using this for a password and just throwing it into an array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜