I am trying to read a number character with character, but I don\'t know if the stdin buffer is empty or not.
My first solution whas to look for \'\\n\' character i
For anyone who comes here from google – easy select solution to check stdin emptyness:
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);
fd_set savefds = readfds;
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
int chr;
int sel_rv = select(1, &readfds, NULL, NULL, &timeout);
if (sel_rv > 0) {
puts("Input:");
while ((chr = getchar()) != EOF) putchar(chr);
} else if (sel_rv == -1) {
perror("select failed");
}
readfds = savefds;
Needs unistd.h, stdlib.h and stdio.h.
Explanation can be found here.
UPD: Thanks DrBeco for noticing that select returns -1 on error -- error handling was added.
Actually, select returns: