Trouble with scanf( "...%[^...]", ...) 
Author Message
 Trouble with scanf( "...%[^...]", ...)

Hi,

When I use the %[...] input field with scanf(), I run into several major
problems.  Sometimes, all inputs after the input to %[] are ignored.  Most
of the time, the *next* scanf() statement is skipped over -- sometimes
several scanf()'s are skipped, depending on how many inputs were "ignored"
after the %[].  Can you tell me what I'm doinf wrong -- or is this a known
bug with the scanf() function?

main()
{
        float  x, y, z;
        char  mod[ 10], fmt[ 25], str1[ 80], str2[ 80];
        int  num;

        printf( "Part I\n------\nString/String = ");
        num = scanf( "%[^/]/%[^\n]", str1, str2);
        printf( "[%d]\t%s\n\t%s\n\n", num, str1, str2);

        /* So the user should enter blablalbla#1/blablabla#2.  But why
           did can't I specify %[^/]%[^\n] without the repetition of the
           extra slash in %[^/]/%[^\n] ?  I tried this, and my second
           input was either ignored or a slash was appended to it */    

        printf( "Part II\n-------\nFloat/String/Float = ");
        num = scanf( "%f/%[^/]/%f", &x, str1, &y);
        printf( "[%d]  %s (%f, %f)\n\n", num, str1, x, y);

        /* The second floating-point number is ignored and the next
           scanf() is interfered with -- it "jumps over it"... */

        printf( "Part III\n--------\nFormat = ");
        gets( mod);
        sprintf( fmt, "%%*d%%*%s%%*f%%f%%f%%f%%*d", mod);
        printf( "Input {%s} = ", fmt);
        num = scanf( fmt, &x, &y, &z);
        printf( "[%d]  x= %f,  y= %f,  z= %f\n", num, x, y, z);

Quote:
}

Thanks in advance!  E-mail preferred.

- Eskandar

--
-------------------------------------------------------------------------
G o d l e s s   f e e l i n g   i n   m e
B o r n   o f   t h e i r   l i e s . . .                         Danzig
-------------------------------------------------------------------------



Wed, 18 Oct 1995 07:51:23 GMT  
 Trouble with scanf( "...%[^...]", ...)

Quote:

> When I use the %[...] input field with scanf(), I run into several major
> problems.  Sometimes, all inputs after the input to %[] are ignored.  Most
> of the time, the *next* scanf() statement is skipped over -- sometimes
> several scanf()'s are skipped, depending on how many inputs were "ignored"
> after the %[].  Can you tell me what I'm doinf wrong -- or is this a known
> bug with the scanf() function?

Your main problem is that %[] must match at least one character.  If
there are no matching character, the conversion fails and scanf stops;
that's why the following inputs are ignored.  Since you haven't
processed the remaining character, following scanf calls try to process
them and they don't match, so those scanfs fail as well.  You should
always check the return value from scanf -- if it's not what you expect,
you're in serious trouble.  Trying to recover from this situation is
usually not easy.
----
Larry Jones, SDRC, 2000 Eastman Dr., Milford, OH  45150-2789  513-576-2070

Everything's gotta have rules, rules, rules! -- Calvin


Thu, 19 Oct 1995 00:00:24 GMT  
 Trouble with scanf( "...%[^...]", ...)

Quote:


> skandar Ensafi) writes:
>> When I use the %[...] input field with scanf(), I run into several major
>> problems.
> Your main problem is that %[] must match at least one character.

Which points up the biggest problem with scanf(). It doesn't handle
unexpected input gracefully.  Personally, I always fgets() into a buffer
and sscanf() the buffer.  Even for the quick'n'dirty stuff.

Someone on c.l.c once posted their usual compiler defines, and my two
favorites were -Dscanf=DONT_USE_SCANF and -Dgets=DONT_USE_GETS.
--

                 "Be careful... or be roadkill"
                                                 -- Calvin



Sun, 22 Oct 1995 15:37:26 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. scanf("%s", string) or scanf("%s", &string)? Both work, yet...

2. "scanf" function with assignment-suppression

3. scanf("%c", &var) behavior!!

4. scanf("%s", &ar[])

5. scanf("%s", &ar[])

6. scanf( "%s" ) question

7. handling improper input to "scanf()"

8. The effect of "\n" in scanf

9. trouble with this "sizeof" macro

10. Win32 "ReadFile()" trouble

11. Troubles with "#define STRICT"

12. Troubles with "lvalue missing" error message

 

 
Powered by phpBB® Forum Software