Scanf being missed?

M

Mike

Hi,

I have some code that contains the line:

scanf("%[^\t\n]s", string);

to read a string containing white spaces.

When tested in a simple program:

#include <stdio.h>
int main()
{
char string[10];
scanf("%[^\t\n]s", string);
printf("%s\n", string);
return 0;
}

it works fine however when written into a part of a larger program I am
writing it's as if I've missed out the line and compiler skips over it.

Stepping through it, it is getting called and wrapping it up i printf()
confirms this.

Snippet of code before and after:

printf("Enter data:\n");
for (i=0; i<=A.Rows; i++)
{
scanf("%[^\t\n]s", string);
for (j=0; j<20; j++)
{
/*Extra code in here*/
}
}

Any ideas?

Thanks,
 
M

Mark Wooding

Mike said:
printf("Enter data:\n");
for (i=0; i<=A.Rows; i++)
{
scanf("%[^\t\n]s", string); [...]
}

Any ideas?

The scanf function doesn't skip whitespace before or after scanning a
scanset. I think what's happening is that it scans the first item, and
leaves the file position just at the terminating tab or newline. So
when you try to scan another item, you find that it's empty -- since the
very next character is excluded from the set -- which causes scanf as a
whole to fail.

You need to munch the separator, perhaps by putting a space in the scanf
control string:

scanf(" %[^\t\n]s ", string);

A space character, or a sequence of them, means `skip zero or more
whitespace characters in the input'. If your format is stricter than
this, or you don't want to much leading and trailing spaces, then you
might have to mess with %c or %*c -- sorry!

Finally, you really ought to put a length limit on the scanset, e.g., if
your buffer is
char string[10];

then

scanf(" %9[^\t\n]s ", string);

(allow space for the null terminator!).

-- [mdw]
 
J

Jens Thoms Toerring

Mike said:
I have some code that contains the line:
scanf("%[^\t\n]s", string);
to read a string containing white spaces.
When tested in a simple program:
#include <stdio.h>
int main()
{
char string[10];
scanf("%[^\t\n]s", string);

I suspect that the 's' after the '[^\t\n]' is not what you
want - the '[^\t\n]' is a replacement for 's' and not an
additional qualifier.

BTW, you could make your use of scanf() a bit safer to set a
maximum number of chards that can be read like in

scanf("%9[^\t\n]", string);

to avoid writting past the end of 'string'.
printf("%s\n", string);
return 0;
}
it works fine however when written into a part of a larger program I am
writing it's as if I've missed out the line and compiler skips over it.
Stepping through it, it is getting called and wrapping it up i printf()
confirms this.
Snippet of code before and after:
printf("Enter data:\n");
for (i=0; i<=A.Rows; i++)
{
scanf("%[^\t\n]s", string);
for (j=0; j<20; j++)
{
/*Extra code in here*/
}
}

When the first read with scanf() ends because a '\t' or '\n'
got found that character is left in the input buffer. Now,
when you then call scanf() again, again asking for charac-
ters not being '\t' or '\n', then the first character found
is what's left in the input buffer - just one of those chars
you just told scanf() not to accept. So you would have to
have either a call of scanf() in between that accepts (or
skips) the offending character or you have to remove it
yourself using e.g. getc().
Regards, Jens
 
G

Guest

Hi,

I have some code that contains the line:

     scanf("%[^\t\n]s", string);

to read a string containing white spaces.

When tested in a simple program:

#include <stdio.h>
int main()
{
    char string[10];
    scanf("%[^\t\n]s", string);
    printf("%s\n", string);
    return 0;

}

it works fine however when written into a part of a larger program I am
writing it's as if I've missed out the line and compiler skips over it.

Stepping through it, it is getting called and wrapping it up i printf()
confirms this.

Snippet of code before and after:

    printf("Enter data:\n");
    for (i=0; i<=A.Rows; i++)
    {
        scanf("%[^\t\n]s", string);
        for (j=0; j<20; j++)
        {
        /*Extra code in here*/
         }
    }

Any ideas?

in general it is a good idea to check the return value of scanf()
(or member of its family). Error recovery can be tricky with scanf()
so you might consider fgets() followed by sscanf().
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,733
Latest member
LonaMonzon

Latest Threads

Top