ScanfFscanfSscanfinput string interpretation
#include <stdio.h>
fscanfFILE *fpchar *controlchar *pointer...scanfchar *controlchar *pointer...sscanfchar *stringchar *controlchar *pointer...Description
These functions perform the complement to "printf()" etc.
Fscanf performs conversions from the file "fp", scanf from the
standard input, and sscanf from the string pointed to by
"string".
Each function expects a control string containing conversion
specifications, and zero or more pointers to objects into which
the converted values are stored.
The control string may contain three types of fields:
Space, tab characters, or "\n" which match any of
the three in the input.
Characters not among the above and not "%" which must
match characters in the input.
A "%" followed by an optional "*" indicates
suppression of assignment, an optional field width
maximum and a conversion character indicating the
type expected.
A conversion character controls the conversion to be applied
to the next field and indicates the type of the corresponding
pointer argument. A field consists of consecutive non-space
characters and ends at either a character inappropiate for the
conversion or when a specified field is exhausted.
When one field is finished, white-space characters are passed
over until the next field is found.
dA decimal string is to be converted to an integer.oAn octal string; the coresponding argument should
point to an integer.xA hexadecimal string for conversion to an integer.sA string of non-space characters is expected and
will be copied to the buffer pointed to by the
corresponding argument and a null ("\0") appended.
The user must ensure that the buffer is large
enough. The input string is considered terminated
by a space, tab of ("\n").cA character is expected and is copied into the byte
pointed to by the argument. The white-space
skipping is suppressed for this conversion. If a
field width is given, the argument is assumed to
point to a character array and the number of
characters indicated is copied to it. NOTE to ensure
that the next non-white-space character is read use
"%1s" and that TWO bytes are pointed to by the
argument.e,fA floating point representation is expected on the
input and the argument must be a pointer to a float.
Any of the usual ways of writing floating point
numbers are recognized.[This denotes the start of a set of match characters;
the inclusion or exclusion of which delimits the
input field. The white-space skipping is
suppressed. The corresponding argument should be a
pointer to a character array. If the first
character in the match string is not "^",
characters are copied from the input as long as they
can be found in the match string. If the first
character is the "^", copying continues while characters
cannot be found in the match string. The match
string is delimited by a "]".D,O,XSimilar to d,o,x above, but the corresponding
argument is considered to point to a long integer.E,FSimilar to e,f above, but the corresponding
should point to a double.%A match for "%" is sought; no conversion takes place.
Each of the functions returns a count of the number of
fields successfully matched and assigned.
Caveats
The returned count of matches/assigments does not include
character matches and assigments suppressed by "*". The
arguments must ALL be pointers. It is a common error to call
scanf with the value of an item rather than a pointer to it.
Diagnostics
These functions return EOF on end of input or error and a count
which is shorter than expected for unexpected or unmatched
items.
See Also
Atoi(), atof(),
getc(),
printf()
Kernighan and Ritchie pp 147-150