 |
sscanf |
Function (Macro) |
String parsing function.
sscanf scans the string buffer
for the formats in the format
string format
and assigns the input to pointers passed as varargs.
Returns:
- the number of pointers filled in (the number of matches done) if it is non-0
- 0 if no pointers were filled in because of a format matching error
- EOF (-1) if the input ended before any pointers were filled in
Formats accepted:
- any non-whitespace character other than '%': matches a literal character,
assigns nothing
- whitespace characters: match any whitespace characters, even if they are
a different kind of whitespace, assign nothing
- '%%': matches a literal '%', assigns nothing
- Any formats of the type '%' + flags + width + type (or '%' + width + flags +
type, the order isn't actually checked, you can even put the flags in the
middle of the width):
Flags accepted:
- '*': skip the matched data (don't assign it to a pointer, and don't count it)
- 'h': if an integer type follows, it will be a short integer, otherwise the
flag is ignored. (This is the default if neither 'h' nor 'l' are
specified.)
- 'l': if an integer type follows, it will be a long integer, otherwise the
flag is ignored
- Warning: "%h" or "%l" alone is not accepted. Always write "%li", "%ld", ...
explicitely.
Width: Maximum number of bytes to read in for data matching. The maximum is
65535. Any larger number will be truncated modulo 65536. If the width is
0 or omitted, the default width for the format is used.
Types accepted:
- 'u': matches an unsigned decimal integer
default width: 65536
required pointer: 'unsigned short *' for '%hu', 'unsigned long *' for '%lu'
automatically skips leading whitespace
- 'd': matches a signed decimal integer (both [-] and [(-)] are accepted)
default width: 65536
required pointer: 'short *' for '%hd', 'long *' for '%ld'
automatically skips leading whitespace
- 'o': matches an unsigned octal integer
default width: 65536
required pointer: 'unsigned short *' for '%ho', 'unsigned long *' for '%lo'
automatically skips leading whitespace
- 'x' or 'X': matches an unsigned hexadecimal integer (0-9 and both a-f and A-F
are accepted)
default width: 65536
required pointer: 'unsigned short *' for '%hx', '%hX', 'unsigned long *' for '%lx', '%lX'
automatically skips leading whitespace
- 'p': same as '%lx' (even if '%hp' or just '%p' is specified)
- 'i': matches an integer in C syntax: may contain a negative sign ([-] or [(-)]),
is hexadecimal if started with 0x, octal if started with 0, and
decimal otherwise
default width: 65536
required pointer: 'short *' or 'unsigned short *' for '%hi', 'long *' or 'unsigned long *' for '%li'
automatically skips leading whitespace
- 'f', 'g', 'e' or 'E': matches a floating-point number. (This number will be
parsed by push_parse_text (through atof), so it has to
use the calculator '-' and 'E'.)
default and maximum width: 29. This limitation is
because we need to allocate a buffer for atof on the
stack, and we didn't want to waste too much stack space.
required pointer: 'float *'
automatically skips leading whitespace
- 's': matches a non-whitespace string, and null-terminates it when copying
it to the buffer (so make sure you have a buffer of size
width + 1
)
default width: 65536
required pointer: 'char *'
automatically skips leading whitespace
- 'c': matches a fixed number of characters (the given width). Does NOT
null-terminate the string when copying it to the buffer.
default width: 1 (NOT the maximum width like for the other formats)
required pointer: 'char *'
does NOT skip leading whitespace (put a space before '%c' if you want
to skip it)
- '[': matches a regexp-style set of characters:
- ']' terminates the set unless it immediately follows the leading '['
or the leading '[^'
- if '^' is the first character, matches the characters which are NOT in
the set
- '-' specifies a range of characters (as in '0-9') unless it
immediately precedes the terminating ']'
- any other characters (including ']' right at the beginning or '-'
right at the end): match the corresponding literal characters
copies characters until either the input ends, or the specified width
is reached, or a character which is not in the specified set (when using
'^': which is in the specified set) is encountered
null-terminates the copied string (so make sure you have a buffer of size
width + 1
)
default width: 65536
required pointer: 'char *'
does not skip leading whitespace (put a space before '%[' if you want
to skip it)
- 'n': does not read anything from the input, but assigns the number of
characters already read in to the given pointer
default width: N/A (the width is ignored, and no characters are read in)
required pointer: 'short *'
does not skip any whitespace in the input
Uses: cbscanf
Used by: scanf