[vox-tech] a better scanf?? (C-programming question)

Andy Campbell vox-tech@lists.lugod.org
Fri, 18 Apr 2003 04:48:50 -0700


>
>
>   Non-blocking with select/poll/busy-looping, reading into a
> buffer, followed by sscanf when a return key is found will certainly
> handle the second case... examples if that's what you want.
>   Otherwise you will have to go with some curses or raw mode input
> to get character by character input from a user, and opens a whole
> can of worms...
>
>     TTFN,
>       Mike


Mike,

I am trying to implement this but am unsuccessfull. Could you show me where 
some examples are?? Thank you!!

Here is my code (it may be obviuos that I am a newbie.....I am trying to use 
file descriptors with stdin but the comiler won't let me. All in all....I 
guess I don't know enough about how you could use stdin as a file 
descriptor....any thoughts??


#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>


char input;
int flag = 0;

int main()
{
        fd_set rfds;
        struct timeval tv;
        int retval;
        int n;
        int key_press;
   
/* 
        int key_press;
     
        if((key_press = open(stdin, O_NONBLOCK)) < 0){
                fprintf(stderr, "Error opening stdin\n");
                exit(1);
        }
*/    Well this didn't work...it wouldn't let me open stdin...perhaps that is 
a naive thing to try

        while(flag == 0)
        {
        
                
                
                FD_ZERO(&rfds);
                FD_SET(stdin, &rfds);       It didn't like this!!
                tv.tv_sec = 1;
                tv.tv_usec = 0;
                
                retval = select(FD_SETSIZE, &rfds, NULL, NULL, &tv);
                
                if (retval > 0) {
                        if (FD_ISSET(stdin, &rfds)) {           ....Or this...
                                n = read(stdin, &input,  1);     .......or 
this........
                                printf("\nInput recorded as %c\n", input);
                                if (input == 'x')
                                        flag = 1;
                        }
                }

                
        
                printf("Waiting...\n");
                //sleep(5);
        }
        
        return 0;
}