2

我有这段代码,用于在终端的登录屏幕上隐藏我的密码。登录后,输入全部还是空白。完成后如何将其设置为正常状态,例如恢复默认值getline

#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);


    return 0;
}//main 
4

1 回答 1

2

You've already saved off the previous terminal state with a get call; now you just have to restore it with a set call in the same manner as you set the new state:

tcsetattr(STDIN_FILENO, TCSANOW, &oldt);

(For future visitors: the second parameter is a flag that means the change will occur immediately.)

于 2013-04-19T18:45:25.887 回答