我正在编写一个Linux CLI程序。我需要从用户那里获取密码,显然,我不希望密码被回显到控制台。
这里有几种解决方案,但它们都适用于普通C.
C command-line password input
How to mask password in c?
Getting a password in C without using getpass (3)?
如何使用std :: string而不是char []?
来适应C ++最优雅的C ++解决方案是什么?
答案 0 :(得分:2)
Linux本身(大部分)是用C语言编写的,所以你在C ++中找到的任何东西都只是一个C例程的抽象。最好自己调用例程,转换输入和结果。
答案 1 :(得分:2)
使用任何普通的C解决方案:
std::string pass (100); // size the string at your max password size (minus one)
func_to_get_pass(&pass[0], pass.size());
// function takes a char* and the max size to write (including a null char)
pass.resize(pass.find('\0'));
cout << "Your password is " << pass << ".\n"; // oops! don't show it ;)