-
November 25th, 2008, 11:53 AM
#1
Reading from stdin (without echo)
Back in the old days I sometimes used to use getch() if I wanted to read user input, one character at a time. getch() is handy because it doesn't echo characters back to stdout. Unfortunately, getch() reads from the console - not from stdin and these aren't guaranteed to be the same thing.
cin can be used to read from stdin - as can getc() and getchar() but when running the app from a console, they all seem to have the disadvantage of automatically echoing their input back to the user. Is there a way to turn this behaviour off?
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
November 25th, 2008, 12:00 PM
#2
Re: Reading from stdin (without echo)
I think the echoing is not echoing as such but more default os behaviour.
Whats happening is as you are typing thats going into a buffer provided by the operating system. On pressing return, that buffer probably gets copied to the internal buffer in cin which then lets you extract whatever you want from that buffer.
Well I think thats whats happening.
Maybe if you peruse the console functions on msdn you will find something applicable for changing default os behaviour to not show typed chars as they are typed but obviously this wouldn't be portable.
-
November 25th, 2008, 12:25 PM
#3
Re: Reading from stdin (without echo)
In Windows, you can turn off echo for any standard input function with SetConsoleMode().
Code:
#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
In *nix you can use the "termios" interface to disable echo.
Code:
#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
gg
-
November 25th, 2008, 12:28 PM
#4
Re: Reading from stdin (without echo)
Russco - I guess it's possible that the OS is doing this rather than the C runtime but I can't think of a way to check
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
November 25th, 2008, 12:30 PM
#5
Re: Reading from stdin (without echo)
Codeplug - thanks, I'll give that a try
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
November 25th, 2008, 12:34 PM
#6
Re: Reading from stdin (without echo)
Originally Posted by John E
Russco - I guess it's possible that the OS is doing this rather than the C runtime but I can't think of a way to check
You can try piping a file to stdin rather than typing into it directly. You'll see the contents of the file never appear on the screen.
-
November 25th, 2008, 01:22 PM
#7
Re: Reading from stdin (without echo)
Originally Posted by Lindley
You can try piping a file to stdin rather than typing into it directly. You'll see the contents of the file never appear on the screen.
Good suggestion.... it must be the C functions that do the echoing, rather than the OS.
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
November 25th, 2008, 02:10 PM
#8
Re: Reading from stdin (without echo)
No, that's the opposite conclusion I'd come to given that evidence....
The C standard library functions never even see the input until you hit return. Prior to that, everything that's happening is on the console's doorstep.
-
November 25th, 2008, 02:57 PM
#9
Re: Reading from stdin (without echo)
Yes, that's possible too - the console itself might be echoing its input. But if that's the case, why doesn't anything get echoed if I use getch()
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
November 25th, 2008, 03:17 PM
#10
Re: Reading from stdin (without echo)
At the least it implies that the echoing is occurring as a result of the console accepting input in a certain mode, rather than in general, which suggests a way to replicate the behavior.....
-
November 25th, 2008, 05:32 PM
#11
Re: Reading from stdin (without echo)
Originally Posted by John E
Yes, that's possible too - the console itself might be echoing its input. But if that's the case, why doesn't anything get echoed if I use getch()
The console is set up by default for line entry. As codeplug showed you to turn it off you have to clear the appropriate flag with SetConsoleMode.
-
November 26th, 2008, 08:33 AM
#12
Re: Reading from stdin (without echo)
Thanks guys. SetConsoleMode worked perfectly.
"A problem well stated is a problem half solved.” - Charles F. Kettering
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|