Michael Roecken wrote:

> Hi.  I was just wondering if there was a function that allows one to
> execute a loop while waiting for input from the keyboard.
> 
> The only way I'm used to it is executing the loop with a scanf for example
> inside the loop.  But this will pause the loop waiting for input.
> 
> Hopefully you understand what I'm saying.
> 
> Thanks,
> Mike
> 
> Example
> 
> while(1)
>       printf ("\a");
> 
> 
> Now while this is looping I want to stop it by exiting with the stroke of
> a single key anytime during this loop.  How would this be done.

Normally, the terminal driver only passes input to the application in
whole lines (e.g. so that you can use BS to delete input). If you want
to receive individual characters as they are typed, you first need to
use tcsetattr() to put the terminal into raw mode.

Next, if you're going to be using the stdio functions to read input,
you should use setvbuf() to disable buffering.

To prevent reads from blocking until input is available, you can
either:

a) Use fcntl() to set the O_NONBLOCK flag. This will cause read() to
return -1 and set errno to EAGAIN.

b) Use select() with a zero timeout before calling read(), to
determine if any input is available, and only call read() if there is.

If you're writing an interactive program, you may be better off using
curses, which handles many of these details for you.

-- 
Glynn Clements <[EMAIL PROTECTED]>

Reply via email to