Re: std::cin and disabling canonical line processing (buffering)



Harold Bamford wrote:
I would like to read from cin one character at a time (eg, cin.get(thisChar) or the like) and not have to wait for the user to enter a carriage return.

On Unix or Cygwin, I would use tcgetattr/tcsetattr to turn off buffering and echoing on file descriptor 0 (STDIN) but this doesn't seem to be available in VC++ 7.1

What's the trick?

You could try something along these lines:

#include <windows.h>
#include <iostream>

int main()
{
    HANDLE inputHandle = GetStdHandle(STD_INPUT_HANDLE);
    BOOL b = SetConsoleMode(inputHandle, 0);

    char c;
    while(std::cin.get(c))
    {
        if (c == 'x')
            break;
        std::cout << c << " typed" << std::endl;
    }
}

Also, look at the rest of the Console functions, which you might prefer to deal with directly.

Tom
.



Relevant Pages

  • Re: What is the more popular UNIX flavor?
    ... about my experience with Solaris and Cygwin. ... installing packages. ... needing eg tftp you only need to activate on a Unix system. ... probably need installing first on the equivalent Windows system. ...
    (comp.unix.questions)
  • Re: Open source mail server on windows, oh my...
    ... > Life under windows would be unbearable without cygwin. ... plus UNIX servers is the answer for servers under Windows. ...
    (comp.os.linux)
  • Re: Open source mail server on windows, oh my...
    ... > Life under windows would be unbearable without cygwin. ... plus UNIX servers is the answer for servers under Windows. ...
    (comp.os.linux.questions)
  • Re: What is the more popular UNIX flavor?
    ... The solaris system I was using went ... experience with Solaris and Cygwin. ... Cygwin is but a thin veneer that makes windows more ... want out of full unix but can't get on cygwin. ...
    (comp.unix.questions)
  • Re: ksh under Cygwin
    ... Try the cygwin mailing list. ... So it's about how to setup a shell on doze at all, ... which is pretty unlikely on unix. ... "What is Linux? ...
    (comp.unix.shell)

Loading