1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
.TH FSCANF 2
.SH NAME
fscanf, scanf, sscanf, vfscanf \- scan formatted input
.SH SYNOPSIS
.B "#include <u.h>
.br
.B "#include <stdio.h>"
.PP
.B
int fscanf(FILE *f, char *format, ...)
.PP
.B
int scanf(char *format, ... )
.PP
.B
int sscanf(char *s, char *format, ...)
.PP
.B
int vfscanf(FILE *stream, char *format, char *args)
.SH DESCRIPTION
.I Fscanf
reads from the named input stream
.I f
(see
.IR fopen (2))
under control of the string pointed to by
.I format
that specifies the admissible input sequences and how they are to be converted
for assignment, using subsequent arguments as pointers to the objects
to receive the converted input.
If there are insufficient arguments for the format, the behavior is undefined.
If the format is exhausted while arguments remain, the excess arguments
are evaluated (as always) but are otherwise ignored.
.PP
.I Scanf
and
.I sscanf
are the same, but they read from
.I stdin
and the character string
.IR s ,
respectively.
.I Vfscanf
is like
.IR scanf ,
except the
.I args
argument is a pointer to an argument in an argument list of the
calling function and the effect is as if the calling function's
argument list from that point on is passed to the scanf routines.
.PP
The format is composed of zero or more directives:
one or more white-space characters; an ordinary character (not
.BR % );
or a conversion specification.
Each conversion specification is introduced by the character
.BR %.
After the
.BR % ,
the following appear in sequence:
.PP
.RS
An optional assignment-suppressing character
.BR * .
.PP
An optional decimal integer that specifies the maximum field width.
.PP
An optional
.BR h ,
.B l
(ell) or
.B L
indicating the size of the receiving object.
The conversion specifiers
.BR d ,
.BR i ,
and
.B n
shall be preceded by
.B h
if the corresponding argument is a pointer to
.B short
rather than a pointer to
.BR int ,
or by
.B l
if it is a pointer to
.BR long .
Similarly, the conversion specifiers
.BR o ,
.BR u ,
and
.B x
shall be preceded by
.B h
if the corresponding argument is a pointer to
.B unsigned
.B short
rather than a pointer to
.BR unsigned ,
or by
.B l
if it is a pointer to
.B unsigned
.BR long .
Finally, the conversion specifiers
.BR e ,
.BR f ,
and
.B g
shall be preceded by
.B l
if the corresponding argument is a pointer to
.B double
rather than a pointer to
.BR float ,
or by
.B L
if it is a pointer to
.B long
.BR double .
If an
.BR h ,
.BR l ,
or
.B L
appears with any other conversion specifier, the behavior is undefined.
.PP
A character that specifies the type of conversion to be applied.
The valid conversion specifiers are described below.
.RE
.PP
.I Fscanf
executes each directive of the format in turn.
If a directive fails, as detailed below,
.I fscanf
returns.
Failures are described as input failures (due to the unavailability
of input),
or matching failures (due to inappropriate input).
.PP
A directive composed of white space is executed by reading input up
to the first non-white-space character (which remains unread),
or until no more characters can be read.
.PP
A directive that is an ordinary character is executed by reading
the next character from the stream.
If if differs from the one comprising the directive,
the directive fails, and the differing and subsequent characters
remain unread.
.PP
A directive that is a conversion specification defines a set of
matching input sequences, as described below for each specifier.
A conversion specification is executed in the following steps:
.PP
Input white-space characters (as specified by
.IR isspace ,
see
.IR ctype (2))
are skipped, unless the specification includes a
.BR [ ,
.BR c ,
or
.B n
specifier.
.PP
An input item is read from the stream,
unless the specification includes an
.B n
specifier.
An input item is defined as the longest sequence of input characters
(up to any specified maximum field width) which is an initial
subsequence of a matching sequence.
The first character, if any, after the input item remains unread.
If the length of the input item is zero, the execution of
the directive fails: this condition is a matching failure,
unless an error prevented input from the stream,
in which case it is an input failure.
.PP
Except in the case of a
.B %
specifier, the input item (or, in the case of a
.B %n
directive, the count of input characters)
is converted to a type appropriate to the conversion specifier.
If the input item is not a matching sequence, the execution of
the directive fails: this condition is a matching failure.
Unless assignment suppression was indicated by a
.BR * ,
the result of the conversion is placed in the object pointed to by the
first argument following the
.I format
argument that has not already received a conversion result.
If this object does not have an appropriate type,
or if the result of the conversion cannot be represented
in the space provided, the behavior is undefined.
.PP
The following conversion specifiers are valid:
.TP 6
.B d
Matches an optionally signed decimal integer,
whose format is the same as expected for the subject sequence
of the
.I strtol
(see
.IR atof (2))
function with 10 for the
.B base
argument.
The corresponding argument shall be a pointer to
.BR int .
.TP
.B i
Matches an optionally signed decimal integer,
whose format is the same as expected for the subject sequence
of the
.I strtol
function with 0 for the
.B base
argument.
The corresponding argument shall be a pointer to
.BR int .
.TP
.B o
Matches an optionally signed octal integer,
whose format is the same as expected for the subject sequence
of the
.I strtoul
(see
.IR atof (2))
function with 8 for the
.B base
argument.
The corresponding argument shall be a pointer to
.B unsigned
.BR int .
.TP
.B u
Matches an optionally signed decimal integer,
whose format is the same as expected for the subject sequence
of the
.I strtoul
function with 10 for the
.B base
argument.
The corresponding argument shall be a pointer to
.B unsigned
.BR int .
.TP
.B x
Matches an optionally signed hexadecimal integer,
whose format is the same as expected for the subject sequence
of the
.I strtoul
function with 16 for the
.B base
argument.
The corresponding argument shall be a pointer to
.B unsigned
.BR int .
.TP
.BR e , f , g
Matches an optionally signed floating-point number, whose format is
the same as expected for the subject string of the
.I strtod
(see
.IR atof (2))
function.
The corresponding argument shall be a pointer to
.BR float .
.TP
.B s
Matches a sequence of non-white-space characters.
The corresponding argument shall be a pointer to the initial
character of an array large enough to accept the sequence
and a terminating NUL (0) character, which will be added automatically.
.TP
.B [
Matches a nonempty sequence of characters from a set of expected
characters (the
.IR scanset ).
The corresponding argument shall be a pointer to the initial
character of an array large enough to accept the sequence and a terminating
NUL character, which will be added automatically.
The conversion specifier includes all subsequent characters in the
.I format
string, up to and including the matching right brace
.RB ( ] ).
The characters between the brackets (the
.IR scanlist )
comprise the scanset, unless the character after the left bracket
is a circumflex
.RB ( ^ ),
in which case the scanset contains all characters that do not appear
in the scanlist between the circumflex and the right bracket.
As a special case, if the conversion specifier begins with
.B []
or
.BR [^] ,
the right bracket character is in the scanlist and the next
right bracket character is the matching right bracket
that ends the specification.
If a
.B -
character is in the scanlist and is not the first, nor the second
where the first character is a
.BR ^ ,
nor the last character, the behavior is implementation-defined
(in Plan 9: the scanlist includes all characters in the
.SM ASCII
(sic)
range between the two characters on either side of the
.BR - ).
.TP
.B c
Matches a sequence of characters of the number specified by the field width
(1 if no field width is present in the directive).
The corresponding argument shall be a pointer to the initial character
of an array large enough to accept the sequence.
No NUL character is added.
.TP
.B P
Matches an implementation-defined set of sequences,
which should be the same as the set of sequences that may be
produced by the
.B %P
conversion of the
.IR fprintf (2)
function
(in Plan 9, a hexadecimal number).
The corresponding argument shall be a pointer to a pointer to
.BR void .
The interpretation of the input item is implementation defined;
however, for any input item other than a value converted earlier
during the same program execution, the behavior of the
.B %P
conversion is undefined.
.TP
.B n
No input is consumed.
The corresponding argument shall be a pointer to integer into which
is written the number of characters read from the input stream so far
by this call to
.IR fscanf .
Execution of a
.B %n
directive does not increment the assignment count returned at the
completion of
.IR fscanf .
.TP
.B %
Matches a single
.BR % ;
no conversion or assignment occurs.
The complete conversion specification shall be
.BR %% .
.PD
.PP
If a conversion specification is invalid, the behavior is undefined.
.PP
The conversion specifiers
.BR E ,
.BR G ,
and
.B X
are also valid and behave the same as, respectively,
.BR e ,
.BR g ,
and
.BR x .
.PP
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.
.PP
If conversion terminates on a conflicting input character,
the offending input character is left unread in the input stream.
Trailing white space (including newline 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
.B %n
directive.
.PP
The return value from
.I fscanf
is the number of input items assigned, which can be fewer than
provided for, or even zero, in the event of an early matching failure.
However, if an input failure occurs before any conversion,
.B EOF
is returned.
.SH SOURCE
.B /sys/src/libstdio
.SH "SEE ALSO"
.IR fopen (2),
.IR fgetc (2)
.SH BUGS
Does not know about
.SM UTF.
|