Problem Reading from console using select
Code:
/*
** select.c -- a select() demo
*/
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <termios.h>
#include <stropts.h>
#define STDIN 0 // file descriptor for standard input
int main(void)
{
struct timeval tv;
fd_set readfds;
fd_set master;
tv.tv_sec = 5;
tv.tv_usec = 500000;
//struct termios term;
FD_ZERO(&master);
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &master);
// don't care about writefds and exceptfds:
while(1){
readfds=master;
/*tcgetattr(STDIN, &term);
term.c_lflag &= ~ICANON;
tcsetattr(STDIN, TCSANOW, &term);
setbuf(stdin, NULL);*/
select(STDIN+1, &readfds, NULL, NULL, NULL);
if (FD_ISSET(STDIN_FILENO, &readfds)){
printf("A key was pressed!\n");
//FD_CLR();
fflush(stdout);
}
else
printf("Timed out.\n");
}
return 0;
}
why prog goes in infinite loop after 1st enter? i want to read single lines just like gets()