std::cin and unbuffering (noncanonical input)
-
Greetings, First, I apologize for what must be in a FAQ somewhere that I haven't located. I have tried. Here is the basic question (details below): How do I turn off canonical input processing on an arbritrary istream? Failing that, how do I do it with just cin using VC++ 7.1? The gory details: I am writing a function that is part of a larger project so I don't have a lot of control over the style of its parameters. It gets an istream and ostream pointer. With the istream pointer I have to read a character at a time as this is for an interactive application; I need to react to each character as it is entered, not after the entire line has been entered. Like an editor. But if the istream is std::cin, then I have canonical line processing (no extra charge!) where the characters are echoed to the user, ^H (backspace) is interpreted, and nothing is available to the calling program until a newline has been entered. If I compile under GCC 3.4.4, I can (with vast pain) determine that the istream is associated with file descriptor 0 (STDIN_FILENO) and use tcgetattr/tcsetattr to turn off the canonical processing. But these functions (tcgetattr/tcsetattr) do not appear to be available in VC++ 7.1 I have looked for days for a general way to deal with this problem and either it is so obvious that nobody mentions it or I have completely missed it. Even the GCC documentation doesn't give a solution for itself (though I found one on the net). The VC++ documentation casually mentions that istream buffering can be turned off, but doesn't say how. Perhaps they were not referring to canonical processing but something else... I have tried some of the obvious stuff already:
// Forcing cin to not do line-buffering and echoing #include #include using namespace std; int main(int argc, char *argv[]) { char inChar; #if 0 // compiles but input is still line-buffered cin.rdbuf()->pubsetbuf(0,0); #endif #if 0 // doesn't compile under cygwin g++ 3.4.4 or VC++ 7.1 // It was mentioned somewhere on the net so I had to try... cin.setbuf(0,0); #endif #if 0 // According to MS, cin is really an ostream (!), so... // compiles but input is still line-buffered (wasn't really expecting much) cin >> unitbuf; #endif #if 0 // compiles but input is still line-buffered (wasn't really expecting much) cin.setf(ios::unitbuf); #endif while(cin) { cin.get(inChar); if(inChar == '\004') break; cout << "\nRead in '" << inChar << "'\n" <<