Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just 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,

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,695
    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!

  3. #3
    Just Joined!
    Join Date
    Oct 2010
    Posts
    4
    Quote Originally Posted by Rubberman View Post
    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.
    Thanks man. But I could only say whether there's any input from Keyboard or not. I couldn't get what's fed from keyboard.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Oct 2010
    Posts
    4
    Quote Originally Posted by cprogdude View Post
    Thanks man. But I could only say whether there's any input from Keyboard or not. I couldn't get what's fed from keyboard.

    Never mind man. I had the wrong macro set in. It's working now. Thanks.

  6. #5
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,695
    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!

  7. #6
    Just Joined!
    Join Date
    Oct 2010
    Posts
    4
    Quote Originally Posted by Rubberman View Post
    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().

    Got it sir. I'm facing another problem right now. I'm unable to flush my input streams. fflush(stdin) failed on me. Any other solution.

    Thanks,

  8. #7
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,695
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •