Is there a way to timeout a reading from stdin in order for the program not to hang for too long ?
read(0, var, numberofbytes);
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use ncurses or if you don’t want to, you can use select as described in this blog post. Basically, you can use
selectand specify the timeout. If the stdin FD is set, then you can read from it safely and won’t block. If you want more info on select, check this out and of course wikipedia. It’s a handy call to know about.EDIT: I feel compelled to supply code, so here it is, straight from the blog post with some comments.