| sscanf |
(String str, String format [, Object obj1, ..., Object objN]) |
yoix.stdio |
| |
Scans
str
for characters matching
format,
stores each match in the corresponding argument (when appropriate),
and returns the number of matches that were successfully stored, or
-1
if there was an error before the first successful match.
The
format
string consists of zero or more components of the following types:
-
ordinary characters that must exactly match characters in
str.
-
white space, any number of which matches any number of white space in the
corresponding relative position in the input string (in other words, if you
match a number, then have 2 spaces in the format, then all white space after
the matched number is discarded from the input string).
-
conversion specifications that determine how characters in
str
are converted into objects.
There should be general agreement between the type of
a conversion specification and the corresponding Object argument.
Each format conversion has the general form,
%[ * ][ ' ][ width ]specification
where the square brackets indicate optional elements and:
-
*
indicates conversion should take place, but
that the actual assignment to the argument should be suppressed.
-
'
indicates that the number will have thousands separators
(e.g., commas) and these should be ignored in the number conversion.
-
width
indicates that only the number of characters specified by
width
should be converted from the input string.
-
specification
is one of:
| c |
matches up to the field width characters (1 by default),
and puts them in a string or an array without a trailing null character.
| | d |
looks for a base-10 integer, which may be signed.
| | e or E |
looks for a base-10 exponential number.
| | f |
looks for a base-10 floating point number.
| | g or G |
looks for a base-10 number.
| | i |
looks for an integer, either base-10, base-8 or base-16
(base-8 is assumed if it starts with "0",
base-16 is assumed if it starts with "0x", otherwise base-10 is assumed).
| | n |
assigns the current number of bytes read from
str
to the Object argument.
| | o |
looks for a base-8 integer.
| | u |
looks for an unsigned base-10 integer.
| | x or X |
looks for a base-16 integer.
| | s |
looks for a string up to the first white space.
| | [...] |
matches the longest non-empty string of characters from the set enclosed
by brackets.
A leading
^
means match characters not listed in the set that immediately follows the
^.
A range of characters can be specified as
first-last.
| | % |
matches a percent sign, but does not assign it.
|
If you are a
C
programmer you probably will not have much trouble with the Yoix version of
sscanf,
but we made one important change that deserves mention.
The code
String buf[100];
sscanf(str, "%s", buf);
works the way you might expect and copies non-white space characters into
buf,
however this
String buf;
sscanf(str, "%s", &buf);
also works, but it is not something you would try in
C.
The Yoix implementation of
sscanf
collects characters from
str
and then takes a careful look at the argument that corresponds to the
%s.
In the first case it is a string,
so all it does is copy the characters into the space that is already been
allocated for
buf.
In the second case the argument is
&buf,
which is a pointer to a string, so this time
sscanf
creates a new string using the characters just read from
str
and then it points
buf
at that new string.
| |
| Example: |
Here is a simple program
import yoix.stdio.*;
String line;
String key;
String value;
while (line = stdin.nextline) {
if (sscanf(line, "%[A-Z]=%[^\n]%*c", &key, &value) == 2)
printf("key=%s, value=%s\n", key, value);
}
that shows how easily
sscanf
can extract key/value pairs from lines that look like,
NAME=Sean Q. Public
ADDRESS=123 Main Street, Anytown, USA
PHONE=888-555-1212
in any string.
| | |
| Return: |
int
| | |
| See Also: |
fprintf,
printf,
scanf,
sprintf,
sscanf
|
|
Yoix is a registered trademark of AT&T Inc.
|