]>
Commit | Line | Data |
---|---|---|
1 | .\" Copyright (c) 1990, 1991, 1993 | |
2 | .\" The Regents of the University of California. All rights reserved. | |
3 | .\" | |
4 | .\" This code is derived from software contributed to Berkeley by | |
5 | .\" Chris Torek and the American National Standards Committee X3, | |
6 | .\" on Information Processing Systems. | |
7 | .\" | |
8 | .\" Redistribution and use in source and binary forms, with or without | |
9 | .\" modification, are permitted provided that the following conditions | |
10 | .\" are met: | |
11 | .\" 1. Redistributions of source code must retain the above copyright | |
12 | .\" notice, this list of conditions and the following disclaimer. | |
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | |
14 | .\" notice, this list of conditions and the following disclaimer in the | |
15 | .\" documentation and/or other materials provided with the distribution. | |
16 | .\" 3. All advertising materials mentioning features or use of this software | |
17 | .\" must display the following acknowledgement: | |
18 | .\" This product includes software developed by the University of | |
19 | .\" California, Berkeley and its contributors. | |
20 | .\" 4. Neither the name of the University nor the names of its contributors | |
21 | .\" may be used to endorse or promote products derived from this software | |
22 | .\" without specific prior written permission. | |
23 | .\" | |
24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
34 | .\" SUCH DAMAGE. | |
35 | .\" | |
36 | .\" @(#)scanf.3 8.2 (Berkeley) 12/11/93 | |
37 | .\" $FreeBSD: src/lib/libc/stdio/scanf.3,v 1.24 2003/06/28 09:03:25 das Exp $ | |
38 | .\" | |
39 | .Dd January 4, 2003 | |
40 | .Dt SCANF 3 | |
41 | .Os | |
42 | .Sh NAME | |
43 | .Nm scanf , | |
44 | .Nm fscanf , | |
45 | .Nm sscanf , | |
46 | .Nm vscanf , | |
47 | .Nm vsscanf , | |
48 | .Nm vfscanf | |
49 | .Nd input format conversion | |
50 | .Sh LIBRARY | |
51 | .Lb libc | |
52 | .Sh SYNOPSIS | |
53 | .In stdio.h | |
54 | .Ft int | |
55 | .Fn scanf "const char * restrict format" ... | |
56 | .Ft int | |
57 | .Fn fscanf "FILE * restrict stream" "const char * restrict format" ... | |
58 | .Ft int | |
59 | .Fn sscanf "const char * restrict str" "const char * restrict format" ... | |
60 | .In stdarg.h | |
61 | .Ft int | |
62 | .Fn vscanf "const char * restrict format" "va_list ap" | |
63 | .Ft int | |
64 | .Fn vsscanf "const char * restrict str" "const char * restrict format" "va_list ap" | |
65 | .Ft int | |
66 | .Fn vfscanf "FILE * restrict stream" "const char * restrict format" "va_list ap" | |
67 | .Sh DESCRIPTION | |
68 | The | |
69 | .Fn scanf | |
70 | family of functions scans input according to a | |
71 | .Fa format | |
72 | as described below. | |
73 | This format may contain | |
74 | .Em conversion specifiers ; | |
75 | the results from such conversions, if any, | |
76 | are stored through the | |
77 | .Em pointer | |
78 | arguments. | |
79 | The | |
80 | .Fn scanf | |
81 | function | |
82 | reads input from the standard input stream | |
83 | .Dv stdin , | |
84 | .Fn fscanf | |
85 | reads input from the stream pointer | |
86 | .Fa stream , | |
87 | and | |
88 | .Fn sscanf | |
89 | reads its input from the character string pointed to by | |
90 | .Fa str . | |
91 | The | |
92 | .Fn vfscanf | |
93 | function | |
94 | is analogous to | |
95 | .Xr vfprintf 3 | |
96 | and reads input from the stream pointer | |
97 | .Fa stream | |
98 | using a variable argument list of pointers (see | |
99 | .Xr stdarg 3 ) . | |
100 | The | |
101 | .Fn vscanf | |
102 | function scans a variable argument list from the standard input and | |
103 | the | |
104 | .Fn vsscanf | |
105 | function scans it from a string; | |
106 | these are analogous to | |
107 | the | |
108 | .Fn vprintf | |
109 | and | |
110 | .Fn vsprintf | |
111 | functions respectively. | |
112 | Each successive | |
113 | .Em pointer | |
114 | argument must correspond properly with | |
115 | each successive conversion specifier | |
116 | (but see the | |
117 | .Cm * | |
118 | conversion below). | |
119 | All conversions are introduced by the | |
120 | .Cm % | |
121 | (percent sign) character. | |
122 | The | |
123 | .Fa format | |
124 | string | |
125 | may also contain other characters. | |
126 | White space (such as blanks, tabs, or newlines) in the | |
127 | .Fa format | |
128 | string match any amount of white space, including none, in the input. | |
129 | Everything else | |
130 | matches only itself. | |
131 | Scanning stops | |
132 | when an input character does not match such a format character. | |
133 | Scanning also stops | |
134 | when an input conversion cannot be made (see below). | |
135 | .Sh CONVERSIONS | |
136 | Following the | |
137 | .Cm % | |
138 | character introducing a conversion | |
139 | there may be a number of | |
140 | .Em flag | |
141 | characters, as follows: | |
142 | .Bl -tag -width ".Cm l No (ell)" | |
143 | .It Cm * | |
144 | Suppresses assignment. | |
145 | The conversion that follows occurs as usual, but no pointer is used; | |
146 | the result of the conversion is simply discarded. | |
147 | .It Cm hh | |
148 | Indicates that the conversion will be one of | |
149 | .Cm dioux | |
150 | or | |
151 | .Cm n | |
152 | and the next pointer is a pointer to a | |
153 | .Vt char | |
154 | (rather than | |
155 | .Vt int ) . | |
156 | .It Cm h | |
157 | Indicates that the conversion will be one of | |
158 | .Cm dioux | |
159 | or | |
160 | .Cm n | |
161 | and the next pointer is a pointer to a | |
162 | .Vt "short int" | |
163 | (rather than | |
164 | .Vt int ) . | |
165 | .It Cm l No (ell) | |
166 | Indicates that the conversion will be one of | |
167 | .Cm dioux | |
168 | or | |
169 | .Cm n | |
170 | and the next pointer is a pointer to a | |
171 | .Vt "long int" | |
172 | (rather than | |
173 | .Vt int ) , | |
174 | that the conversion will be one of | |
175 | .Cm a , e , f , | |
176 | or | |
177 | .Cm g | |
178 | and the next pointer is a pointer to | |
179 | .Vt double | |
180 | (rather than | |
181 | .Vt float ) , | |
182 | or that the conversion will be one of | |
183 | .Cm c , | |
184 | .Cm s | |
185 | or | |
186 | .Cm \&[ | |
187 | and the next pointer is a pointer to an array of | |
188 | .Vt wchar_t | |
189 | (rather than | |
190 | .Vt char ) . | |
191 | .It Cm ll No (ell ell) | |
192 | Indicates that the conversion will be one of | |
193 | .Cm dioux | |
194 | or | |
195 | .Cm n | |
196 | and the next pointer is a pointer to a | |
197 | .Vt "long long int" | |
198 | (rather than | |
199 | .Vt int ) . | |
200 | .It Cm L | |
201 | Indicates that the conversion will be one of | |
202 | .Cm a , e , f , | |
203 | or | |
204 | .Cm g | |
205 | and the next pointer is a pointer to | |
206 | .Vt "long double" . | |
207 | .It Cm j | |
208 | Indicates that the conversion will be one of | |
209 | .Cm dioux | |
210 | or | |
211 | .Cm n | |
212 | and the next pointer is a pointer to a | |
213 | .Vt intmax_t | |
214 | (rather than | |
215 | .Vt int ) . | |
216 | .It Cm t | |
217 | Indicates that the conversion will be one of | |
218 | .Cm dioux | |
219 | or | |
220 | .Cm n | |
221 | and the next pointer is a pointer to a | |
222 | .Vt ptrdiff_t | |
223 | (rather than | |
224 | .Vt int ) . | |
225 | .It Cm z | |
226 | Indicates that the conversion will be one of | |
227 | .Cm dioux | |
228 | or | |
229 | .Cm n | |
230 | and the next pointer is a pointer to a | |
231 | .Vt size_t | |
232 | (rather than | |
233 | .Vt int ) . | |
234 | .It Cm q | |
235 | (deprecated.) | |
236 | Indicates that the conversion will be one of | |
237 | .Cm dioux | |
238 | or | |
239 | .Cm n | |
240 | and the next pointer is a pointer to a | |
241 | .Vt "long long int" | |
242 | (rather than | |
243 | .Vt int ) . | |
244 | .El | |
245 | .Pp | |
246 | In addition to these flags, | |
247 | there may be an optional maximum field width, | |
248 | expressed as a decimal integer, | |
249 | between the | |
250 | .Cm % | |
251 | and the conversion. | |
252 | If no width is given, | |
253 | a default of | |
254 | .Dq infinity | |
255 | is used (with one exception, below); | |
256 | otherwise at most this many bytes are scanned | |
257 | in processing the conversion. | |
258 | In the case of the | |
259 | .Cm lc , | |
260 | .Cm ls | |
261 | and | |
262 | .Cm l[ | |
263 | conversions, the field width specifies the maximum number | |
264 | of multibyte characters that will be scanned. | |
265 | Before conversion begins, | |
266 | most conversions skip white space; | |
267 | this white space is not counted against the field width. | |
268 | .Pp | |
269 | The following conversions are available: | |
270 | .Bl -tag -width XXXX | |
271 | .It Cm % | |
272 | Matches a literal | |
273 | .Ql % . | |
274 | That is, | |
275 | .Dq Li %% | |
276 | in the format string | |
277 | matches a single input | |
278 | .Ql % | |
279 | character. | |
280 | No conversion is done, and assignment does not occur. | |
281 | .It Cm d | |
282 | Matches an optionally signed decimal integer; | |
283 | the next pointer must be a pointer to | |
284 | .Vt int . | |
285 | .It Cm i | |
286 | Matches an optionally signed integer; | |
287 | the next pointer must be a pointer to | |
288 | .Vt int . | |
289 | The integer is read in base 16 if it begins | |
290 | with | |
291 | .Ql 0x | |
292 | or | |
293 | .Ql 0X , | |
294 | in base 8 if it begins with | |
295 | .Ql 0 , | |
296 | and in base 10 otherwise. | |
297 | Only characters that correspond to the base are used. | |
298 | .It Cm o | |
299 | Matches an octal integer; | |
300 | the next pointer must be a pointer to | |
301 | .Vt "unsigned int" . | |
302 | .It Cm u | |
303 | Matches an optionally signed decimal integer; | |
304 | the next pointer must be a pointer to | |
305 | .Vt "unsigned int" . | |
306 | .It Cm x , X | |
307 | Matches an optionally signed hexadecimal integer; | |
308 | the next pointer must be a pointer to | |
309 | .Vt "unsigned int" . | |
310 | .It Cm a , A , e , E , f , F , g , G | |
311 | Matches a floating-point number in the style of | |
312 | .Xr strtod 3 . | |
313 | The next pointer must be a pointer to | |
314 | .Vt float | |
315 | (unless | |
316 | .Cm l | |
317 | or | |
318 | .Cm L | |
319 | is specified.) | |
320 | .It Cm s | |
321 | Matches a sequence of non-white-space characters; | |
322 | the next pointer must be a pointer to | |
323 | .Vt char , | |
324 | and the array must be large enough to accept all the sequence and the | |
325 | terminating | |
326 | .Dv NUL | |
327 | character. | |
328 | The input string stops at white space | |
329 | or at the maximum field width, whichever occurs first. | |
330 | .Pp | |
331 | If an | |
332 | .Cm l | |
333 | qualifier is present, the next pointer must be a pointer to | |
334 | .Vt wchar_t , | |
335 | into which the input will be placed after conversion by | |
336 | .Xr mbrtowc 3 . | |
337 | .It Cm S | |
338 | The same as | |
339 | .Cm ls . | |
340 | .It Cm c | |
341 | Matches a sequence of | |
342 | .Em width | |
343 | count | |
344 | characters (default 1); | |
345 | the next pointer must be a pointer to | |
346 | .Vt char , | |
347 | and there must be enough room for all the characters | |
348 | (no terminating | |
349 | .Dv NUL | |
350 | is added). | |
351 | The usual skip of leading white space is suppressed. | |
352 | To skip white space first, use an explicit space in the format. | |
353 | .Pp | |
354 | If an | |
355 | .Cm l | |
356 | qualifier is present, the next pointer must be a pointer to | |
357 | .Vt wchar_t , | |
358 | into which the input will be placed after conversion by | |
359 | .Xr mbrtowc 3 . | |
360 | .It Cm C | |
361 | The same as | |
362 | .Cm lc . | |
363 | .It Cm \&[ | |
364 | Matches a nonempty sequence of characters from the specified set | |
365 | of accepted characters; | |
366 | the next pointer must be a pointer to | |
367 | .Vt char , | |
368 | and there must be enough room for all the characters in the string, | |
369 | plus a terminating | |
370 | .Dv NUL | |
371 | character. | |
372 | The usual skip of leading white space is suppressed. | |
373 | The string is to be made up of characters in | |
374 | (or not in) | |
375 | a particular set; | |
376 | the set is defined by the characters between the open bracket | |
377 | .Cm [ | |
378 | character | |
379 | and a close bracket | |
380 | .Cm ] | |
381 | character. | |
382 | The set | |
383 | .Em excludes | |
384 | those characters | |
385 | if the first character after the open bracket is a circumflex | |
386 | .Cm ^ . | |
387 | To include a close bracket in the set, | |
388 | make it the first character after the open bracket | |
389 | or the circumflex; | |
390 | any other position will end the set. | |
391 | The hyphen character | |
392 | .Cm - | |
393 | is also special; | |
394 | when placed between two other characters, | |
395 | it adds all intervening characters to the set. | |
396 | To include a hyphen, | |
397 | make it the last character before the final close bracket. | |
398 | For instance, | |
399 | .Ql [^]0-9-] | |
400 | means the set | |
401 | .Dq "everything except close bracket, zero through nine, and hyphen" . | |
402 | The string ends with the appearance of a character not in the | |
403 | (or, with a circumflex, in) set | |
404 | or when the field width runs out. | |
405 | .Pp | |
406 | If an | |
407 | .Cm l | |
408 | qualifier is present, the next pointer must be a pointer to | |
409 | .Vt wchar_t , | |
410 | into which the input will be placed after conversion by | |
411 | .Xr mbrtowc 3 . | |
412 | .It Cm p | |
413 | Matches a pointer value (as printed by | |
414 | .Ql %p | |
415 | in | |
416 | .Xr printf 3 ) ; | |
417 | the next pointer must be a pointer to | |
418 | .Vt void . | |
419 | .It Cm n | |
420 | Nothing is expected; | |
421 | instead, the number of characters consumed thus far from the input | |
422 | is stored through the next pointer, | |
423 | which must be a pointer to | |
424 | .Vt int . | |
425 | This is | |
426 | .Em not | |
427 | a conversion, although it can be suppressed with the | |
428 | .Cm * | |
429 | flag. | |
430 | .El | |
431 | .Pp | |
432 | The decimal point | |
433 | character is defined in the program's locale (category | |
434 | .Dv LC_NUMERIC ) . | |
435 | .Pp | |
436 | For backwards compatibility, a | |
437 | .Dq conversion | |
438 | of | |
439 | .Ql %\e0 | |
440 | causes an immediate return of | |
441 | .Dv EOF . | |
442 | .Sh RETURN VALUES | |
443 | These | |
444 | functions | |
445 | return | |
446 | the number of input items assigned, which can be fewer than provided | |
447 | for, or even zero, in the event of a matching failure. | |
448 | Zero | |
449 | indicates that, while there was input available, | |
450 | no conversions were assigned; | |
451 | typically this is due to an invalid input character, | |
452 | such as an alphabetic character for a | |
453 | .Ql %d | |
454 | conversion. | |
455 | The value | |
456 | .Dv EOF | |
457 | is returned if an input failure occurs before any conversion such as an | |
458 | end-of-file occurs. | |
459 | If an error or end-of-file occurs after conversion | |
460 | has begun, | |
461 | the number of conversions which were successfully completed is returned. | |
462 | .Sh SEE ALSO | |
463 | .Xr getc 3 , | |
464 | .Xr mbrtowc 3 , | |
465 | .Xr printf 3 , | |
466 | .Xr strtod 3 , | |
467 | .Xr strtol 3 , | |
468 | .Xr strtoul 3 , | |
469 | .Xr wscanf 3 | |
470 | .Sh STANDARDS | |
471 | The functions | |
472 | .Fn fscanf , | |
473 | .Fn scanf , | |
474 | .Fn sscanf , | |
475 | .Fn vfscanf , | |
476 | .Fn vscanf | |
477 | and | |
478 | .Fn vsscanf | |
479 | conform to | |
480 | .St -isoC-99 . | |
481 | .Sh BUGS | |
482 | Earlier implementations of | |
483 | .Nm | |
484 | treated | |
485 | .Cm \&%D , \&%E , \&%F , \&%O | |
486 | and | |
487 | .Cm \&%X | |
488 | as their lowercase equivalents with an | |
489 | .Cm l | |
490 | modifier. | |
491 | In addition, | |
492 | .Nm | |
493 | treated an unknown conversion character as | |
494 | .Cm \&%d | |
495 | or | |
496 | .Cm \&%D , | |
497 | depending on its case. | |
498 | This functionality has been removed. | |
499 | .Pp | |
500 | Numerical strings are truncated to 512 characters; for example, | |
501 | .Cm %f | |
502 | and | |
503 | .Cm %d | |
504 | are implicitly | |
505 | .Cm %512f | |
506 | and | |
507 | .Cm %512d . | |
508 | .Pp | |
509 | The | |
510 | .Cm %n$ | |
511 | modifiers for positional arguments are not implemented. | |
512 | .Pp | |
513 | The | |
514 | .Nm | |
515 | family of functions do not correctly handle multibyte characters in the | |
516 | .Fa format | |
517 | argument. |