I want to detect whether or not there is input waiting on stdin in Windows.
I use the following generic structure on Linux:
fd_set currentSocketSet;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
int result = 1;
while (result > 0){
FD_ZERO(¤tSocketSet);
FD_SET(STDIN, ¤tSocketSet);
result = select(STDIN+1, ¤tSocketSet, NULL, NULL, &tv);
if (result == -1){
printf("Network Error -- select errored with id=%d.
", errno);
return;
} else if (result > 0){
//...do stuff
}
}
Note: I do not want to deal with the keyboard and keyboard functions like kbhit. I want a way to do what I asked. I do not want a third party library to do this either, I would like a native Windows library call to get the answer.
Note #2: On windows the above code fails with windows error code 10038 "An operation was attempted on something that is not a socket" which probably means that windows does not support selecting on STDIN.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…