using select to poll cin

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • timekeer9
    New Member
    • Dec 2006
    • 1

    using select to poll cin

    Hi,
    I would like to poll stdin for input so I can get user input from the command line without blocking. Looking through the archives, it appears that the best way to do this is using a combination of select and cin.rdbuf()->in_avail(). However, I can't seem to get it to work properly. My select statement seems to always return true, and the cin.rdbuf()->in_avail() always returns false, regardless of if the user has typed or not. Here is the relevant code snippet, anyone have any thoughts? (I am using c++ in .net on a windows box if that helps.)

    Code:
    fd_set readfds;
    FD_ZERO(&readfds);
    FD_SET(fileno(stdin), &readfds);
    
    struct timeval tv;
    tv.tv_sec = 0;
    tv.tv_usec = 0;
    	
    
    while(true)
    {
        select(16, &readfds, 0, 0, &tv);
        //if something is available on cin, read in a char
        if(FD_ISSET( fileno(stdin), &readfds ) != 0)
        {
            //process all available input, once character at a time
            while(cin.rdbuf()->in_avail()>0)
            { 
                char c;
    	    cin.get(c);
                processUserInput(c);
    	}
    }
    Thanks!
Working...