|
|
int scanf(const char *format, ...);
int fscanf(FILE *strm, const char *format, ...);
int sscanf(const char *s, const char *format, ...);
The fscanf() function reads from the stream strm.
The sscanf() function reads from the character string s.
Each function reads characters, interprets them according to a format, and stores the results in its arguments. Each expects, as arguments, a control string, format, described below and a set of pointer arguments indicating where the converted input should be stored. If there are insufficient arguments for the format, the behavior is undefined. If the format is exhausted while arguments remain, the excess arguments are simply ignored.
The control string usually contains conversion specifications, which are used to direct interpretation of input sequences. The control string may contain:
The following table defines which size indicators can used with which conversion codes, and the size they indicate.
Conversion Code Size Indicator Size
d, i, n none signed int
h signed short
l signed long
ll signed long long
o, u, x none unsigned int
h unsigned short
l unsigned long
ll unsigned long long
e, f, g none float
l double
L long double
The h, l, ll, or L modifier is ignored with any other conversion codes.
A conversion specification directs the conversion of the next input field; the result is placed in the variable pointed to by the corresponding argument unless assignment suppression was indicated by the character *. The suppression of assignment provides a way of describing an input field that is to be skipped. An input field is defined as a string of non-space characters; it extends to the next inappropriate character or until the maximum field width, if one is specified, is exhausted. For all descriptors except the character [ and the character c, white space leading an input field is ignored.
Conversions can be applied to the nth argument in the argument list, rather than to the next unused argument. In this case, the conversion character % (see above) is replaced by the sequence %digits$ where digits is a decimal integer n, giving the position of the argument in the argument list. The first such argument, %1$, immediately follows format. The control string can contain either form of a conversion specification, that is, % or %digits$, although the two forms cannot be mixed within a single control string.
The conversion code indicates the interpretation of the input field; the corresponding pointer argument must usually be of a restricted type. For a suppressed field, no pointer argument is given. The following conversion codes are valid:
A range of characters in the scanset may be represented by the construct first - last; thus [0123456789] may be expressed [0-9]. Using this convention, first must be lexically less than or equal to last, or else the dash will stand for itself. The character - will also stand for itself whenever it is the first or the last character in the scanlist. To include the right bracket as an element of the scanset, it must appear as the first character (possibly preceded by a circumflex) of the scanlist and in this case it will not be syntactically interpreted as the closing bracket. At least one character must match for this conversion to be considered successful.
If an invalid conversion character follows the %, the results of the operation may not be predictable.
The conversion specifiers E, G, and X are also valid and, under the -Xa and -Xc compilation modes (see cc.1b behave the same as e, g, and x, respectively. Under the -Xt compilation mode, E, G, and X behave the same as le, lg, and lx, respectively.
Each function allows for detection of a language-dependent decimal point character in the input string. The decimal point character is defined by the program's locale (category LC_NUMERIC). In the "C" locale, or in a locale where the decimal point character is not defined, the decimal point character defaults to a period (.).
The scanf() conversion terminates at end of file, at the end of the control string, or when an input character conflicts with the control string.
If end-of-file is encountered during input, conversion is terminated. If end-of-file occurs before any characters matching the current directive have been read (other than leading white space, where permitted), execution of the current directive terminates with an input failure; otherwise, unless execution of the current directive is terminated with a matching failure, execution of the following directive (if any) is terminated with an input failure.
If conversion terminates on a conflicting input character, the
offending input character is left unread in the input stream.
Trailing white space (including new-line characters) is left
unread unless matched by a directive.
The success of literal matches and suppressed assignments is not
directly determinable other than via the %n directive.
int i, n; float x; char name[50];
n = scanf ("%d%f%s", &i, &x, name);
with the input line:
25 54.32E-1 thompson
will assign to n the value 3, to i the value 25, to x the value 5.432, and name will contain thompson\0.
The call to the function scanf():
int i; float x; char name[50];
(void) scanf ("%2d%f%*d %[0-9]", &i, &x, name);
with the input line:
56789 0123 56a72
will assign 56 to i, 789.0 to x, skip 0123, and place the characters 56\0 in name. The next character read from stdin will be a.
+---------------+-----------------+ |ATTRIBUTE TYPE | ATTRIBUTE VALUE | +---------------+-----------------+ |MT-Level | MT-Safe | |CSI | Enabled | +---------------+-----------------+
|
|
Created by unroff & hp-tools. © by Hans-Peter Bischof. All Rights Reserved (1997).
Last modified 07/October/97