scanf() help

S

Stu

I have the following "C" program, which works fine.

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

int
main()
{
char *buffer = "1234 - 5678";
int n, from, to;

n = sscanf (buffer, "%d%*s%d", &from, &to);

printf ("n = %d from = %d, to = %d\n", n, from, to);

return (0);
}

When I change the vaule of buffer to char *buffer = "1234-5678"; (note
I removed the spaces between '-') I need to change my scanf() to

n = sscanf (buffer, "%d%*c%d", &from, &to); /*change from %s to %c */

Can somebody please provide me with one scanf() statement that can handle
both spaces and non spaces between my values. The man page on Solaris did not
provide me with much direction thats why I am posting my question here.


Thanks in advance to all that answer
 
E

Eric Sosman

Stu said:
I have the following "C" program, which works fine.

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

int
main()
{
char *buffer = "1234 - 5678";
int n, from, to;

n = sscanf (buffer, "%d%*s%d", &from, &to);

printf ("n = %d from = %d, to = %d\n", n, from, to);

return (0);
}

When I change the vaule of buffer to char *buffer = "1234-5678"; (note
I removed the spaces between '-') I need to change my scanf() to

n = sscanf (buffer, "%d%*c%d", &from, &to); /*change from %s to %c */

Can somebody please provide me with one scanf() statement that can handle
both spaces and non spaces between my values. The man page on Solaris did not
provide me with much direction thats why I am posting my question here.

It sounds like you need "%*[- ]" (perhaps with additional
"don't care" characters listed). Note that since '-' is one
of the characters you want to skip, there'll be no way to get
a negative `to' value.
 
C

Chris Torek

Stu wrote:
[snippage]
char *buffer = "1234 - 5678";
int n, from, to;

n = sscanf (buffer, "%d%*s%d", &from, &to); [or]
When I change the vaule of buffer to char *buffer = "1234-5678"; ...
n = sscanf (buffer, "%d%*c%d", &from, &to); /*change from %s to %c */

Can somebody please provide me with one scanf() statement that can handle
both spaces and non spaces between my values. ...

It sounds like you need "%*[- ]" (perhaps with additional
"don't care" characters listed). Note that since '-' is one
of the characters you want to skip, there'll be no way to get
a negative `to' value.

This will do the trick, but note that the %[ directive matches
*any* nonempty sequence of *any* of the characters in the scanset
(here just '-' and ' '), so this matches:

1234---5678

and:

1234 - -- - 5678

and:

1234- -5678

(which, as Eric Sosman noted, means there will be no negative "to"
values).

Another alternative is to use the scanf engine's white-space-matching
directives, e.g.:

n = sscanf(buffer, "%d\t%*c\b%d", &from, &to); /* odd way to write it */

Here the whitespace in the format -- \t and \b characters, which
is why it is odd since most people would just use a blank -- tells
scanf to read and ignore zero or more white-space characters in
the input (the string in the buffer, in this case).

The second whitespace directive is actually redundant, because %d
skips initial whitespace automatically anyway, so a more minimal
(and less odd) form is:

n = sscanf(buffer, "%d %*c%d", &from, &to);

If you would like to *require* that the single eaten-up character
be a hyphen, you can just put in a literal hyphen:

n = sscanf(buffer, "%d -%d", &from, &to);

The blank after the first "%d" reads and discards any arbitrary
amount of whitespace that follows the first "%d" conversion, the
"-" then matches only a literal '-' character, and the second %d
reads the second number (first reading and discarding any arbitrary
amount of whitespace, as usual). A final, more-symmetrical looking
variant is:

n = sscanf(buffer, "%d - %d", &from, &to);

which redundantly (but harmlessly) asks the scanf engine to skip
whitespace twice.
 

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,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top