Thread: To unblock scanf call

  1. #1
    Daring to be a guru
    Join Date
    Dec 2007
    Posts
    14

    To unblock scanf call

    im gettin input from the user and the process stops there until the user enter anything is there anyway to move out of scanf if the user doesnt enters anything for a particular time??

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Probably yes, but exactly how would depend on your OS/Compiler.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Added to what Salem says, there's a whole heap of "interesting" stuff connected to timing the users input.

    You really need to understand what you're actually trying to achieve. Does the user have 3 seconds to enter something, or 3 seconds to start typing, then infinite amount of time, or 3 seconds per letter, or ...

    It's not very hard to set something up that "kills" the application after a set amount of time, but it's hard to create something that is "usef friendly" and timed.

    And you probably will want to use a specific function that reads a string, then call sscanf() on the resulting string, rather than try to make this happen within scanf() itself [as you have no control over what happens within scanf itself].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by gopal_85 View Post
    im gettin input from the user and the process stops there until the user enter anything is there anyway to move out of scanf if the user doesnt enters anything for a particular time??
    What platform?

  5. #5
    Daring to be a guru
    Join Date
    Dec 2007
    Posts
    14
    it is in linux platform and using gcc compiler. what i want to do is if the user doesnt enters anything for 10 seconds i should move to the next statement of the code after the scanf.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by gopal_85 View Post
    it is in linux platform and using gcc compiler. what i want to do is if the user doesnt enters anything for 10 seconds i should move to the next statement of the code after the scanf.
    You could use select(), but there are problems, see below:

    Code:
    #include <sys/time.h>
    #include <sys/types.h>
    #include <unistd.h>
    
    #include <stdio.h>
    
    int wait_for_stdin(int seconds)
    {
        fd_set set;
        struct timeval timeout = { seconds, 0 };
    
        FD_ZERO(&set);
        FD_SET(0, &set);
        return select(1, &set, NULL, NULL, &timeout) == 1;
    }
    
    /* Demo */
    int main()
    {
       int val;
       if(!wait_for_stdin(3))
       {
          printf("Timeout\n");
       }
       else
       {
          scanf("%d", &val);
          printf("%d\n", val);
       }
       return 0;
    }
    Then call wait_for_stdin(x) before calling scanf(). If it returns true, the user typed something and you can go ahead and call scanf() to get it. If it returns false, it timed out. The big problem is buffering. There is buffering in the terminal device driver itself which means that until the user actually presses enter, the call to select() will not detect that the user has pressed a key. You'd have to completely turn off terminal line buffering in order to detect individual keypresses.

    This might be sufficient for what you want, though.

  7. #7
    Daring to be a guru
    Join Date
    Dec 2007
    Posts
    14
    thanks buddy. but what you think about the ioctl() function in the termio.h would it help in any ways, there is a option to set the MIN and TIME value in that??

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sure, you can set the timeouts with ioctl(). Just remember to restore before you exit your program!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Daring to be a guru
    Join Date
    Dec 2007
    Posts
    14
    but im little confused with the TIME there it is sayin that it is intercharacter timer what it means and how this ioctl() works???

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by gopal_85 View Post
    but im little confused with the TIME there it is sayin that it is intercharacter timer what it means and how this ioctl() works???
    ioctl() is a very raw interface to the terminal. The advantage of select() is that it's extremely simple and doesn't involve manipulating terminal attributes, although it has the line buffering downside. If you really want to throw the terminal into an unbuffered mode, you should probably consider using ncurses, as it is far more portable than ioctl() calls.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 08:35 AM
  2. scanf interactive program
    By rocketman03 in forum C Programming
    Replies: 9
    Last Post: 11-14-2008, 09:54 PM
  3. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  4. Replies: 1
    Last Post: 06-10-2008, 08:38 PM
  5. Replies: 2
    Last Post: 02-20-2005, 01:48 PM