CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    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

  2. #2
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    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.

  3. #3
    Join Date
    Nov 2003
    Posts
    1,902

    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

  4. #4
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    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

  5. #5
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    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

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Reading from stdin (without echo)

    Quote Originally Posted by John E View Post
    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.

  7. #7
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Reading from stdin (without echo)

    Quote Originally Posted by Lindley View Post
    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

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  9. #9
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    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

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.....

  11. #11
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Reading from stdin (without echo)

    Quote Originally Posted by John E View Post
    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.

  12. #12
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    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
  •  





Click Here to Expand Forum to Full Width

Featured