C++ 在密码提示下隐藏用户输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6899025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:51:00  来源:igfitidea点击:

Hide user input on password prompt

c++consoleiostreamcoutcin

提问by dom0

Possible Duplicate:
Read a password from std::cin

可能重复:
从 std::cin 读取密码

I don't work normally with the console, so my question is maybe very easy to answer or impossible to do .

我不能正常使用控制台,所以我的问题可能很容易回答或不可能做到。

Is it possible to "decouple" cinand cout, so that what I type into the console doesn't appear directly in it again?

是否可以“解耦”cincout,以便我在控制台中输入的内容不会再次直接出现在其中?

I need this for letting the user typing a password and neither me nor the user normally wants his password appearing in plaintexton the screen.

我需要这个来让用户输入密码,我和用户通常都不希望他的密码出现在plaintext屏幕上。

I tried using std::cin.tieon a stringstream, but everything I type is still mirrored in the console.

我尝试std::cin.tie在 a 上使用stringstream,但我输入的所有内容仍会反映在控制台中。

回答by

From How to Hide Text:

如何隐藏文本

Windows

视窗

#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

int main()
{
    HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); 
    DWORD mode = 0;
    GetConsoleMode(hStdin, &mode);
    SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT));

    string s;
    getline(cin, s);

    cout << s << endl;
    return 0;
}//main 

cleanup:

清理:

SetConsoleMode(hStdin, mode);

tcsetattr(STDIN_FILENO, TCSANOW, &oldt);

Linux

Linux

#include <iostream>
#include <string>
#include <termios.h>
#include <unistd.h>

using namespace std;

int main()
{
    termios oldt;
    tcgetattr(STDIN_FILENO, &oldt);
    termios newt = oldt;
    newt.c_lflag &= ~ECHO;
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);

    string s;
    getline(cin, s);

    cout << s << endl;
    return 0;
}//main 

回答by James Kanze

You're really asking about two unrelated issues.
Calling cin.tie( NULL )decouples std::cinand std::coutcompletely. But it doesn't affect anything at a lower level. And at the lowest level, at least under Windows and Unix, std::cinand std::coutare both connected to the same device at the system level, and it is that device (/dev/ttyunder Unix) which does the echoing; you can even redirect standard out to a file, and the console will still echo input.

你真的在问两个不相关的问题。
调用cin.tie( NULL )解耦std::cinstd::cout完全。但它不会影响较低级别的任何内容。而在最底层,至少在 Windows 和 Unix 下,std::cin并且std::cout在系统级别都连接到同一设备,并且是该设备(/dev/tty在 Unix 下)进行回显;你甚至可以将标准输出重定向到一个文件,控制台仍然会回显输入。

How you turn off this echoing depends on the system; the easiest solution is probably to use some sort of third party library, like curses or ncurses, which provides a higher level interface, and hides all the system dependencies.

如何关闭这种回声取决于系统;最简单的解决方案可能是使用某种第三方库,如 curses 或 ncurses,它们提供更高级别的接口,并隐藏所有系统依赖项。

回答by zw324

Use getch()to get the input instead of using cin, so the input will not be shown (quoting wiki):

使用getch()以获得输入,而不是使用的cin,所以输入将不会被显示(引用维基):

int getch(void) Reads a character directly from the console without buffer, and without echo.

int getch(void) 直接从控制台读取一个字符,没有缓冲区,没有回显。

This is really C, not C++, but it might suit you.

这确实是 C,而不是 C++,但它可能适合您。

Also, there's another linkhere.

此外,这里还有另一个链接