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



Tom,

Well I tried this and it almost works. Unfortunately, it looks like
newline/carriage-return filtering is still happening. I can get any character
immediately except for \r or \n. I cannot seem to get a newline (^J) no
matter what, and 2 consecutive \r chars are needed to get one response out of
std::cin.get(c)

I've looked throughout the console function documentation and I cannot find
any reference to turning this behaviour off.

Sorry to be such a pain, but I could still use another clue.

Thanks.

-- Harold


"Tom Widmer [VC++ MVP]" wrote:

> 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: parse error before = token
    ... > us the previous character was the last one. ... the first 1024 bytes or so (in binary mode) and scan for control ... lots of null chars, control chars, or high bit set chars -> binary data ... Now I have left a nit for someone else to pick... ...
    (comp.lang.c)
  • Re: someone
    ... > I have to write a function that reads a number up to 20 chars long from the ... This loop reverses the order of the digits. ... Program: Request character from user. ... Return 2nd char in buffer. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Qt - appending single/multiple chars into QTextEdit
    ... Ideally the chars would be rendered in different ... Use fixed fonts for fast calculations and implement your own ... widget using QWidget and QScrollArea. ... to store the character and its source. ...
    (comp.programming)
  • Re: Person Name 64 chars? What if the name is longer?
    ... behavior if person name is longer than 64 chars? ...  Should you truncate the name? ... I'm aware of some applications that will silently truncate, ... Second, if you're dealing with east asian character sets (Japan, ...
    (comp.protocols.dicom)
  • Re: "i" character regexp madness
    ... // unknowingly high code range ... endpoints of a range do not contain just one character then ... hypotheses) would be working for all chars but "i". ... | unknown or unrepresentable in Unicode. ...
    (comp.lang.javascript)

Loading