Results 1 to 7 of 7
Hi,
I would like to read an input from keyboard using getchar. However, if no input (No Carriage return/new line none whatsoever) is given after say, 5 seconds, I would ...
- 10-01-2010 #1Just Joined!
- Join Date
- Oct 2010
- Posts
- 4
How to skip getchar in C?
Hi,
I would like to read an input from keyboard using getchar. However, if no input (No Carriage return/new line none whatsoever) is given after say, 5 seconds, I would like to skip the getchar and move on. How do I do this in C. I'm using GNU compiler set.
Thanks,
- 10-01-2010 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Usually you would use select() with a timeout. You can also bake your own using an interrupt timer and signal handler, but select() with timeout is better, IMO. I've done it both ways, which depending upon the situation. The nice thing about select() is that it can wait for input from more than one socket or file descriptor at a time, so you can wait for keyboard input as well as a message from the network or elsewhere.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 10-01-2010 #3Just Joined!
- Join Date
- Oct 2010
- Posts
- 4
- 10-01-2010 #4Just Joined!
- Join Date
- Oct 2010
- Posts
- 4
- 10-02-2010 #5Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Ok. The select() function will only tell you that there is data to be read. You still need to read it. I would suggest using read() instead of getchar().
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 10-04-2010 #6Just Joined!
- Join Date
- Oct 2010
- Posts
- 4
- 10-06-2010 #7Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Have you tried putting the streams (stdin) into unbuffered mode (using tcsetattr() functions)? Also, the tc...() functions use fd's, not FILE*. So, how are you trying to flush your input streams?
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote
