scanf hangs when taking multiple inputs in a char array

Discussion in 'C Programming' started by Adi, Nov 3, 2006.

  1. Adi

    Adi Guest

    Hello friend,
    Lately, i cam accross a small problem that has causing quite more
    pain.
    Scanf hangs whenever i try to re- take input into the same char array.
    I've pasted below excerpt of my code:
    ----------------------------------------------------------------------------------------------------------------------------------------
    short cond =0;
    unsigned char intStr[CLI_MAX_INT16_CHARS];


    /*loops until the value entered is valid*/
    do
    {
    fflush(stdin);
    scanf("%s",intStr);
    cond=validateSInt(intStr);
    }while(cond==1);
    ----------------------------------------------------------------------------------------------------------------------------------------
    Given below is the stack trace of my code when i preempted it on SIGINT
    (Ctrl+C). As u can see, the stack dump shows that scanf is hanged....

    (gdb) where
    #0 0x001e016e in __read_nocancel () from /lib/tls/libc.so.6
    #1 0x0017cbb8 in _IO_file_read_internal () from /lib/tls/libc.so.6
    #2 0x0017be8e in _IO_new_file_underflow () from /lib/tls/libc.so.6
    #3 0x0017e5cd in _IO_default_uflow_internal () from /lib/tls/libc.so.6
    #4 0x0017e27d in __uflow () from /lib/tls/libc.so.6
    #5 0x00168cb4 in _IO_vfscanf_internal () from /lib/tls/libc.so.6
    #6 0x0016e67a in scanf () from /lib/tls/libc.so.6
     
    Adi, Nov 3, 2006
    #1
    1. Advertising

  2. Adi

    Chris Dollin Guest

    Adi wrote:

    > Hello friend,
    > Lately, i cam accross a small problem that has causing quite more
    > pain.
    > Scanf hangs whenever i try to re- take input into the same char array.
    > I've pasted below excerpt of my code:


    It's better to paste entire (minimal) examples. (If you don't understand
    what's going wrong, you don't know what's important ...)

    > ----------------------------------------------------------------------------------------------------------------------------------------
    > short cond =0;
    > unsigned char intStr[CLI_MAX_INT16_CHARS];
    >
    >
    > /*loops until the value entered is valid*/
    > do
    > {
    > fflush(stdin);


    Undefined. BOOM. See the FAQ. To discard any "pending input", read
    it.

    > scanf("%s",intStr);


    No check for value being there. Suspect incorrect understanding of %s
    format in scanf. Boomlet.

    > cond=validateSInt(intStr);
    > }while(cond==1);
    > ----------------------------------------------------------------------------------------------------------------------------------------
    > Given below is the stack trace of my code when i preempted it on SIGINT
    > (Ctrl+C). As u can see, the stack dump shows that scanf is hanged....


    No, it shows it was in scanf when you interrupted it.

    --
    Chris "unhashedup hashed up hashing" Dollin
    "Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/
     
    Chris Dollin, Nov 3, 2006
    #2
    1. Advertising

  3. Adi <> wrote:
    > Hello friend,
    > Lately, i cam accross a small problem that has causing quite more
    > pain.
    > Scanf hangs whenever i try to re- take input into the same char array.
    > I've pasted below excerpt of my code:
    > ---------------------------------------------------------------
    > short cond =0;
    > unsigned char intStr[CLI_MAX_INT16_CHARS];


    > /*loops until the value entered is valid*/
    > do
    > {
    > fflush(stdin);


    That's not allowed - you can only fflush() output, but not
    input streams. There seems to be some extension under Windows,
    allowing you to remove everthing from stdin by calling fflush()
    on it, but you should avoid that as non-portable. If you use
    it under e.g. UNIX it probably has not effect at all.

    > scanf("%s",intStr);


    Here's a problem: if the user enters more than (CLI_MAX_INT16_CHARS -1)
    non-white-space characters scanf() will write past the end the buffer
    you supplied it with.

    > cond=validateSInt(intStr);
    > }while(cond==1);
    > ------------------------------------------------------------------
    > Given below is the stack trace of my code when i preempted it on SIGINT
    > (Ctrl+C). As u can see, the stack dump shows that scanf is hanged....


    > (gdb) where
    > #0 0x001e016e in __read_nocancel () from /lib/tls/libc.so.6
    > #1 0x0017cbb8 in _IO_file_read_internal () from /lib/tls/libc.so.6
    > #2 0x0017be8e in _IO_new_file_underflow () from /lib/tls/libc.so.6
    > #3 0x0017e5cd in _IO_default_uflow_internal () from /lib/tls/libc.so.6
    > #4 0x0017e27d in __uflow () from /lib/tls/libc.so.6
    > #5 0x00168cb4 in _IO_vfscanf_internal () from /lib/tls/libc.so.6
    > #6 0x0016e67a in scanf () from /lib/tls/libc.so.6


    This doesn't indicate that you're hanging in scanf(). It's just that
    only while the program is waiting for user input you have a chance to
    hit Ctrl-C and thus it stops somewhere in the innards of scanf().
    Did you check if validSInt() ever returns anything else than 1?

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Nov 3, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc.

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 16, 2006, in forum: C Programming
    Replies:
    185
    Views:
    3,449
    those who know me have no need of my name
    Apr 3, 2006
  2. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Apr 26, 2006, in forum: C Programming
    Replies:
    18
    Views:
    689
    Richard Bos
    May 2, 2006
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,098
    Ian Collins
    May 9, 2006
  4. Replies:
    3
    Views:
    745
  5. Jim Cain
    Replies:
    1
    Views:
    218
    Yukihiro Matsumoto
    Jul 18, 2003
Loading...

Share This Page