Why is it a standard stream can receive input from a terminal in canonical mode, but you put it in raw mode and suddenly this method is no longer valid? I'm well aware of POSIX serial programming, and typically you use read
. I'm trying to understand standard streams better.
#include <termios.h>
#include <unistd.h>
#include <iostream>
termios original;
void enableRawMode() {
tcgetattr(STDIN_FILENO, &original);
termios raw = original;
cfmakeraw(&raw);
raw.c_cc[VMIN] = 0;
raw.c_cc[VTIME] = 1;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}
int main() {
enableRawMode();
char c;
// This works as expected.
//int nread;
//while ((nread = read(STDIN_FILENO, &c, 1)) != 1) {
// if (nread == -1 && errno != EAGAIN) {
// break;
// }
//}
// This loops forever, the failbit is always true, gcount is always 0.
while (!(std::cin.get(c))) {
if (std::cin.bad() || std::cin.eof()) {
break;
}
std::cin.clear();
}
tcsetattr(STDIN_FILENO, TCSAFLUSH, &original);
}