ronys
ronys

Reputation: 518

How to control stdin echo in C++?

In Linux, I can use tcsetattr, but I want to write a portable C++ program that can turn echo of cin on and off (for entering a password). Is there something in std::io* that supports this?

Upvotes: 1

Views: 945

Answers (2)

Sneftel
Sneftel

Reputation: 41474

No, there is not. C/C++'s IO libraries are based around the "stream" model, where input comes from some random source of characters (generally the console) and output is similarly sent to some random character target. In a sense, it isn't C/C++ doing the echoing at all -- it's the console system -- so there's no way for it to control whether the echoing occurs.

Upvotes: 2

Slava
Slava

Reputation: 44238

Unfortunately there is no portable way to disable console echo, so you have to use OS specific API. You can use preprocessor to write portable program, but you would have to write separate code for supported OS and wrap it into #ifdef condition. Another solution would be to use portable library if such one exists, that would do this under the hood for you.

Upvotes: 2

Related Questions