473,386 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.)

Expand|Select|Wrap|Line Numbers
  1. fd_set readfds;
  2. FD_ZERO(&readfds);
  3. FD_SET(fileno(stdin), &readfds);
  4.  
  5. struct timeval tv;
  6. tv.tv_sec = 0;
  7. tv.tv_usec = 0;
  8.  
  9.  
  10. while(true)
  11. {
  12.     select(16, &readfds, 0, 0, &tv);
  13.     //if something is available on cin, read in a char
  14.     if(FD_ISSET( fileno(stdin), &readfds ) != 0)
  15.     {
  16.         //process all available input, once character at a time
  17.         while(cin.rdbuf()->in_avail()>0)
  18.         { 
  19.             char c;
  20.         cin.get(c);
  21.             processUserInput(c);
  22.     }
  23. }
  24.  
Thanks!
Dec 5 '06 #1
0 3569

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: rh0dium | last post by:
Hi all, Another newbie question. So you can't use signals on threads but you can use select. The reason I want to do this in the first place it I need a timeout. Fundamentally I want to run a...
1
by: Magnus Lycka | last post by:
I'm trying to read standard out in a process started with popen2 in a non-blocking way. (Other good ways of doing this than the one I tried are appreciated.) I've tried to dumb down my code to...
1
by: John Pote | last post by:
Hi all, I have the standard Python 2.4.4 windows download installed on my win XP box. But the poll class in module select is not available. The docs indicate that it can work with windows as...
0
by: Maxim Veksler | last post by:
Hi, I'm trying to write a non blocking socket port listener based on poll() because select is limited to 1024 fd. Here is the code, it never gets to "I did not block" until I do a telnet...
10
by: Michele Simionato | last post by:
I have always been curious about how people implement mainloops (or, in Twisted terminology, reactors). So I sit down and I wrote the following simple implementation: import itertools class...
0
by: Sophie000 | last post by:
I have some questions about select() and poll(). I heard the poll( ... ) call is usually preferred over and more efficient than select( ... ) simply because poll( ... ) only requires that you...
2
by: inhahe | last post by:
select.poll isn't supported on Windows, because Windows doesn't have such a feature, or at least it didn't until Vista. Vista implements the same thing but called WSAPoll, an article is here...
1
by: Jean-Paul Calderone | last post by:
On Tue, 6 May 2008 08:36:28 -0400, inhahe <inhahe@gmail.comwrote: If you use Twisted, then you can use I/O Completion Ports, which are even better than WSAPoll, and your code will also work with...
1
by: shapper | last post by:
Hello, I have two tables: Polls PollID, Question Options OptionID, Answer Given a OptionID I want to get the poll to which the Option "Belongs": Poll poll = database.Polls.Select(p...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.