
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
-------------------------------------------------------------------------