473,320 Members | 1,713 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,320 software developers and data experts.

Using select() instead of scanf() for stdin?

I'm trying to do non-blocking I/O in straight C so I can get my app to accept input from the console while appearing to perform other tasks at the same time. I've done a small MUD server before, and select() was great for checking for input each loop so my program didn't wait on new data to read. My question is, can I use select() this way for stdin, or is there some other way to do it?
Mar 27 '07 #1
7 10494
horace1
1,510 Expert 1GB
I'm trying to do non-blocking I/O in straight C so I can get my app to accept input from the console while appearing to perform other tasks at the same time. I've done a small MUD server before, and select() was great for checking for input each loop so my program didn't wait on new data to read. My question is, can I use select() this way for stdin, or is there some other way to do it?
this tends to be operating system dependent. For example, many windows compilers come with a none standard header file <conio.h>, see
http://en.wikipedia.org/wiki/Conio.h
which contains a function kbhit() which returns true if the keyboard has been hit, e.g.
Expand|Select|Wrap|Line Numbers
  1.  while(....)    // some loop
  2.      {
  3.       if (kbhit())
  4.           {
  5.             // keyboard has been hit, read character etc
  6.            ..
  7.          }
  8.      .. other processing within the loop
  9.     }
Mar 27 '07 #2
Well dang. Another source redirected me to using the "Boost" library, which is multiplatform and I think will solve my problem. Apart from POSIX and win32 it looks like libraries are the only way to accomplish this. Thanks for your help.
Mar 27 '07 #3
sicarie
4,677 Expert Mod 4TB
Well dang. Another source redirected me to using the "Boost" library, which is multiplatform and I think will solve my problem. Apart from POSIX and win32 it looks like libraries are the only way to accomplish this. Thanks for your help.
Have you considered using 'getline()'?

http://www.crasseux.com/books/ctutorial/getline.html

(I think it's standard....)
Mar 27 '07 #4
bcarey
5
I just wanted to thank horace1 for putting the example in.
I used your example and added getch function from conio.h and I am now styling!

Thanks again for the post

The one I put together is below:

1. #include <stdio.h>
2. #include <string.h>
3. #include <ctype.h>
4. #include <conio.h>
5. int readinput()
6. {
7. if(kbhit())
8. {
9. // Get next character
10. ic = getch();
11. // do the rest of my code to process char received
12. . . . . .
13. }
14. // Do the rest of my application
15. . . . . .
16. }
Sep 2 '09 #5
JosAH
11,448 Expert 8TB
@bcarey
The "modus operandi" for conio.h is not to use it: it is not portable and most of the time it only works on Microsoft Windows PCs; feel free to be bound to those computers and that operating system.

kind regards,

Jos
Sep 2 '09 #6
bcarey
5
I would like to make my code transportable to Linux in the future, do you have a suggestion on how I can read from the keyboard that won't block and doesn't require a return to continue processing in main program? I was originally going to use a thread and setup an event and event handler but am trying to keep it simple for the people that are going to be using and maintaining it.

Thanks in advance for your time with any suggestion or input.
Sep 3 '09 #7
JosAH
11,448 Expert 8TB
@bcarey
For all different *nixes you can use a curses or an ncurses library or, as was already suggested, you can use stdin in a select function call.

kind regards,

Jos
Sep 3 '09 #8

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

Similar topics

2
by: Kevin | last post by:
Hi All, I am new to MS SQL Server.I am using MS SQL 2000.I have a problem in creating a table by using Select command.I have table called "test" and i want to create another table with the same...
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...
8
by: skinnybloke | last post by:
Hi - I have a problem with a memo field being truncated to about 255 characters when running a Access 2002 query. This only seems to happen if I use SELECT DISTINCT. It works ok using SELECT by...
4
by: Jim Hammond | last post by:
It would be udeful to be able to get the current on-screen values from a FormView that is databound to an ObjectDataSource by using a callback instead of a postback. For example: public void...
0
by: aPRSComp | last post by:
Hi, Has anyone seen where using SELECT * truncates characters ? mysql> SELECT * FROM countries; +-------------+-----------+ | country | capital | +-------------+-----------+...
4
by: satish | last post by:
Values of two columns in two different tables--presentation using select Hi Everyone, i have two tables in the database . One is called address table and one is adressPhone Table. Below...
3
by: ifmusic | last post by:
Hi!, i have a "server-like" application: i use select to accept and recieve data from Sockets. I want to Add a menu to this app something like "pick an option" thing, but it has to show data...
3
by: boliches | last post by:
I am trying to display data using SELECT on "Form Open" to text boxes, but am failing! ie: Data = "SELECT * FROM tblInvoice" Combo1 = Combo1.RowSource = Data Combo1 = Combo1.ItemData(0) Text2...
3
by: bpw22az | last post by:
I am currently in the process of setting up an asp page that sends an inquiring student an email regarding his/her application status. The student enters his/her email address on a web page, the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.