logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

fscanf.3p (24952B)


  1. '\" et
  2. .TH FSCANF "3P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
  3. .\"
  4. .SH PROLOG
  5. This manual page is part of the POSIX Programmer's Manual.
  6. The Linux implementation of this interface may differ (consult
  7. the corresponding Linux manual page for details of Linux behavior),
  8. or the interface may not be implemented on Linux.
  9. .\"
  10. .SH NAME
  11. fscanf,
  12. scanf,
  13. sscanf
  14. \(em convert formatted input
  15. .SH SYNOPSIS
  16. .LP
  17. .nf
  18. #include <stdio.h>
  19. .P
  20. int fscanf(FILE *restrict \fIstream\fP, const char *restrict \fIformat\fP, ...);
  21. int scanf(const char *restrict \fIformat\fP, ...);
  22. int sscanf(const char *restrict \fIs\fP, const char *restrict \fIformat\fP, ...);
  23. .fi
  24. .SH DESCRIPTION
  25. The functionality described on this reference page is aligned with the
  26. ISO\ C standard. Any conflict between the requirements described here and the
  27. ISO\ C standard is unintentional. This volume of POSIX.1\(hy2017 defers to the ISO\ C standard.
  28. .P
  29. The
  30. \fIfscanf\fR()
  31. function shall read from the named input
  32. .IR stream .
  33. The
  34. \fIscanf\fR()
  35. function shall read from the standard input stream
  36. .IR stdin .
  37. The
  38. \fIsscanf\fR()
  39. function shall read from the string
  40. .IR s .
  41. Each function reads bytes, interprets them according to a format, and
  42. stores the results in its arguments. Each expects, as arguments, a
  43. control string
  44. .IR format
  45. described below, and a set of
  46. .IR pointer
  47. arguments indicating where the converted input should be stored. The
  48. result is undefined if there are insufficient arguments for the
  49. format. If the format is exhausted while arguments remain, the excess
  50. arguments shall be evaluated but otherwise ignored.
  51. .P
  52. Conversions can be applied to the
  53. .IR n th
  54. argument after the
  55. .IR format
  56. in the argument list, rather than to the next unused argument. In this
  57. case, the conversion specifier character
  58. .BR %
  59. (see below) is replaced by the sequence \fR"%\fIn\fR$"\fR, where
  60. .IR n
  61. is a decimal integer in the range [1,{NL_ARGMAX}].
  62. This feature provides for the definition of format strings that select
  63. arguments in an order appropriate to specific languages. In format
  64. strings containing the \fR"%\fIn\fR$"\fR form of conversion
  65. specifications,
  66. it is unspecified whether numbered arguments in the argument list can
  67. be referenced from the format string more than once.
  68. .P
  69. The
  70. .IR format
  71. can contain either form of a conversion specification\(emthat is,
  72. .BR %
  73. or \fR"%\fIn\fR$"\fR\(embut the two forms cannot be mixed
  74. within a single
  75. .IR format
  76. string. The only exception to this is that
  77. .BR %%
  78. or
  79. .BR %*
  80. can be mixed with the \fR"%\fIn\fR$"\fR form. When numbered argument
  81. specifications are used, specifying the
  82. .IR N th
  83. argument requires that all the leading arguments, from the first to
  84. the (\c
  85. .IR N \-1)th,
  86. are pointers.
  87. .P
  88. The
  89. \fIfscanf\fR()
  90. function in all its forms shall allow detection of a language-dependent
  91. radix character in the input string. The radix character is defined in
  92. the current locale (category
  93. .IR LC_NUMERIC ).
  94. In the POSIX locale, or in a locale where the radix character is not
  95. defined, the radix character shall default to a
  96. <period>
  97. (\c
  98. .BR '.' ).
  99. .P
  100. The format is a character string, beginning and ending in its initial
  101. shift state, if any, composed of zero or more directives. Each
  102. directive is composed of one of the following:
  103. one or more white-space characters (\c
  104. <space>,
  105. <tab>,
  106. <newline>,
  107. <vertical-tab>,
  108. or
  109. <form-feed>);
  110. an ordinary character (neither
  111. .BR '%'
  112. nor a white-space character); or a conversion specification. Each
  113. conversion specification is introduced by the character
  114. .BR '%'
  115. or the character sequence \fR"%\fIn\fR$"\fR,
  116. after which the following appear in sequence:
  117. .IP " *" 4
  118. An optional assignment-suppressing character
  119. .BR '*' .
  120. .IP " *" 4
  121. An optional non-zero decimal integer that specifies the maximum field
  122. width.
  123. .IP " *" 4
  124. An optional assignment-allocation character
  125. .BR 'm' .
  126. .IP " *" 4
  127. An option length modifier that specifies the size of the receiving
  128. object.
  129. .IP " *" 4
  130. A
  131. .IR "conversion specifier"
  132. character that specifies the type of conversion to be applied. The
  133. valid conversion specifiers are described below.
  134. .P
  135. The
  136. \fIfscanf\fR()
  137. functions shall execute each directive of the format in turn. If a
  138. directive fails, as detailed below, the function shall return. Failures
  139. are described as input failures (due to the unavailability of input
  140. bytes) or matching failures (due to inappropriate input).
  141. .P
  142. A directive composed of one or more white-space characters shall be
  143. executed by reading input until no more valid input can be read, or up
  144. to the first byte which is not a white-space character, which remains
  145. unread.
  146. .P
  147. A directive that is an ordinary character shall be executed as follows:
  148. the next byte shall be read from the input and compared with the byte
  149. that comprises the directive; if the comparison shows that they are not
  150. equivalent, the directive shall fail, and the differing and subsequent
  151. bytes shall remain unread. Similarly, if end-of-file, an encoding
  152. error, or a read error prevents a character from being read, the
  153. directive shall fail.
  154. .P
  155. A directive that is a conversion specification defines a set of
  156. matching input sequences, as described below for each conversion
  157. character. A conversion specification shall be executed in the
  158. following steps.
  159. .P
  160. Input white-space characters (as specified by
  161. .IR "\fIisspace\fR\^(\|)")
  162. shall be skipped, unless the conversion specification includes a
  163. .BR [ ,
  164. .BR c ,
  165. .BR C ,
  166. or
  167. .BR n
  168. conversion specifier.
  169. .P
  170. An item shall be read from the input, unless the conversion
  171. specification includes an
  172. .BR n
  173. conversion specifier. An input item shall be defined as the longest
  174. sequence of input bytes (up to any specified maximum field width, which
  175. may be measured in characters or bytes dependent on the conversion
  176. specifier) which is an initial subsequence of a matching sequence. The
  177. first byte, if any, after the input item shall remain unread. If the
  178. length of the input item is 0, the execution of the conversion
  179. specification shall fail; this condition is a matching failure, unless
  180. end-of-file, an encoding error, or a read error prevented input from
  181. the stream, in which case it is an input failure.
  182. .P
  183. Except in the case of a
  184. .BR %
  185. conversion specifier, the input item (or, in the case of a
  186. .BR %n
  187. conversion specification, the count of input bytes) shall be converted
  188. to a type appropriate to the conversion character. If the input item is
  189. not a matching sequence, the execution of the conversion specification
  190. fails; this condition is a matching failure. Unless assignment
  191. suppression was indicated by a
  192. .BR '*' ,
  193. the result of the conversion shall be placed in the object pointed to
  194. by the first argument following the
  195. .IR format
  196. argument that has not already received a conversion result if the
  197. conversion specification is introduced by
  198. .BR % ,
  199. or in the
  200. .IR n th
  201. argument if introduced by the character sequence \fR"%\fIn\fR$"\fR.
  202. If this object does not have an appropriate type, or if the result of
  203. the conversion cannot be represented in the space provided, the
  204. behavior is undefined.
  205. .P
  206. The
  207. .BR %c ,
  208. .BR %s ,
  209. and
  210. .BR %[
  211. conversion specifiers shall accept an optional assignment-allocation
  212. character
  213. .BR 'm' ,
  214. which shall cause a memory buffer to be allocated to hold the string
  215. converted including a terminating null character. In such a case,
  216. the argument corresponding to the conversion specifier should be a
  217. reference to a pointer variable that will receive a pointer to the
  218. allocated buffer. The system shall allocate a buffer as if
  219. \fImalloc\fR()
  220. had been called. The application shall be responsible for freeing the
  221. memory after usage. If there is insufficient memory to allocate a buffer,
  222. the function shall set
  223. .IR errno
  224. to
  225. .BR [ENOMEM]
  226. and a conversion error shall result. If the function returns EOF, any
  227. memory successfully allocated for parameters using assignment-allocation
  228. character
  229. .BR 'm'
  230. by this call shall be freed before the function returns.
  231. .br
  232. .P
  233. The length modifiers and their meanings are:
  234. .IP "\fRhh\fR" 8
  235. Specifies that a following
  236. .BR d ,
  237. .BR i ,
  238. .BR o ,
  239. .BR u ,
  240. .BR x ,
  241. .BR X ,
  242. or
  243. .BR n
  244. conversion specifier applies to an argument with type pointer to
  245. .BR "signed char"
  246. or
  247. .BR "unsigned char" .
  248. .IP "\fRh\fR" 8
  249. Specifies that a following
  250. .BR d ,
  251. .BR i ,
  252. .BR o ,
  253. .BR u ,
  254. .BR x ,
  255. .BR X ,
  256. or
  257. .BR n
  258. conversion specifier applies to an argument with type pointer to
  259. .BR "short"
  260. or
  261. .BR "unsigned short" .
  262. .IP "\fRl\fR\ (ell)" 8
  263. Specifies that a following
  264. .BR d ,
  265. .BR i ,
  266. .BR o ,
  267. .BR u ,
  268. .BR x ,
  269. .BR X ,
  270. or
  271. .BR n
  272. conversion specifier applies to an argument with type pointer to
  273. .BR "long"
  274. or
  275. .BR "unsigned long" ;
  276. that a following
  277. .BR a ,
  278. .BR A ,
  279. .BR e ,
  280. .BR E ,
  281. .BR f ,
  282. .BR F ,
  283. .BR g ,
  284. or
  285. .BR G
  286. conversion specifier applies to an argument with type pointer to
  287. .BR double ;
  288. or that a following
  289. .BR c ,
  290. .BR s ,
  291. or
  292. .BR [
  293. conversion specifier applies to an argument with type pointer to
  294. .BR wchar_t .
  295. If the
  296. .BR 'm'
  297. assignment-allocation character is specified, the conversion applies
  298. to an argument with the type pointer to a pointer to
  299. .BR wchar_t .
  300. .IP "\fRll\fR\ (ell-ell)" 8
  301. .br
  302. Specifies that a following
  303. .BR d ,
  304. .BR i ,
  305. .BR o ,
  306. .BR u ,
  307. .BR x ,
  308. .BR X ,
  309. or
  310. .BR n
  311. conversion specifier applies to an argument with type pointer to
  312. .BR "long long"
  313. or
  314. .BR "unsigned long long" .
  315. .IP "\fRj\fR" 8
  316. Specifies that a following
  317. .BR d ,
  318. .BR i ,
  319. .BR o ,
  320. .BR u ,
  321. .BR x ,
  322. .BR X ,
  323. or
  324. .BR n
  325. conversion specifier applies to an argument with type pointer to
  326. .BR intmax_t
  327. or
  328. .BR uintmax_t .
  329. .IP "\fRz\fR" 8
  330. Specifies that a following
  331. .BR d ,
  332. .BR i ,
  333. .BR o ,
  334. .BR u ,
  335. .BR x ,
  336. .BR X ,
  337. or
  338. .BR n
  339. conversion specifier applies to an argument with type pointer to
  340. .BR size_t
  341. or the corresponding signed integer type.
  342. .IP "\fRt\fR" 8
  343. Specifies that a following
  344. .BR d ,
  345. .BR i ,
  346. .BR o ,
  347. .BR u ,
  348. .BR x ,
  349. .BR X ,
  350. or
  351. .BR n
  352. conversion specifier applies to an argument with type pointer to
  353. .BR ptrdiff_t
  354. or the corresponding
  355. .BR unsigned
  356. type.
  357. .IP "\fRL\fR" 8
  358. Specifies that a following
  359. .BR a ,
  360. .BR A ,
  361. .BR e ,
  362. .BR E ,
  363. .BR f ,
  364. .BR F ,
  365. .BR g ,
  366. or
  367. .BR G
  368. conversion specifier applies to an argument with type pointer to
  369. .BR "long double" .
  370. .P
  371. If a length modifier appears with any conversion specifier other than
  372. as specified above, the behavior is undefined.
  373. .P
  374. The following conversion specifiers are valid:
  375. .IP "\fRd\fP" 8
  376. Matches an optionally signed decimal integer, whose format is the same
  377. as expected for the subject sequence of
  378. \fIstrtol\fR()
  379. with the value 10 for the
  380. .IR base
  381. argument. In the absence of a size modifier, the application shall
  382. ensure that the corresponding argument is a pointer to
  383. .BR int .
  384. .IP "\fRi\fP" 8
  385. Matches an optionally signed integer, whose format is the same as
  386. expected for the subject sequence of
  387. \fIstrtol\fR()
  388. with 0 for the
  389. .IR base
  390. argument. In the absence of a size modifier, the application shall
  391. ensure that the corresponding argument is a pointer to
  392. .BR int .
  393. .IP "\fRo\fP" 8
  394. Matches an optionally signed octal integer, whose format is the same as
  395. expected for the subject sequence of
  396. \fIstrtoul\fR()
  397. with the value 8 for the
  398. .IR base
  399. argument. In the absence of a size modifier, the application shall
  400. ensure that the corresponding argument is a pointer to
  401. .BR unsigned .
  402. .IP "\fRu\fP" 8
  403. Matches an optionally signed decimal integer, whose format is the same
  404. as expected for the subject sequence of
  405. \fIstrtoul\fR()
  406. with the value 10 for the
  407. .IR base
  408. argument. In the absence of a size modifier, the application shall
  409. ensure that the corresponding argument is a pointer to
  410. .BR unsigned .
  411. .IP "\fRx\fP" 8
  412. Matches an optionally signed hexadecimal integer, whose format is the
  413. same as expected for the subject sequence of
  414. \fIstrtoul\fR()
  415. with the value 16 for the
  416. .IR base
  417. argument. In the absence of a size modifier, the application shall
  418. ensure that the corresponding argument is a pointer to
  419. .BR unsigned .
  420. .IP "\fRa\fR,\ \fRe\fR,\ \fRf\fR,\ \fRg\fR" 8
  421. .br
  422. Matches an optionally signed floating-point number, infinity, or NaN,
  423. whose format is the same as expected for the subject sequence of
  424. \fIstrtod\fR().
  425. In the absence of a size modifier, the application shall ensure that
  426. the corresponding argument is a pointer to
  427. .BR float .
  428. .RS 8
  429. .P
  430. If the
  431. \fIfprintf\fR()
  432. family of functions generates character string representations for
  433. infinity and NaN (a symbolic entity encoded in floating-point
  434. format) to support IEEE\ Std\ 754\(hy1985, the
  435. \fIfscanf\fR()
  436. family of functions shall recognize them as input.
  437. .RE
  438. .IP "\fRs\fP" 8
  439. Matches a sequence of bytes that are not white-space characters. If the
  440. .BR 'm'
  441. assignment-allocation character is not specified, the application shall
  442. ensure that the corresponding argument is a pointer to the initial byte
  443. of an array of
  444. .BR char ,
  445. .BR "signed char" ,
  446. or
  447. .BR "unsigned char"
  448. large enough to accept the sequence and a terminating null character
  449. code, which shall be added automatically.
  450. Otherwise, the application shall ensure that the corresponding
  451. argument is a pointer to a pointer to a
  452. .BR char .
  453. .RS 8
  454. .P
  455. If an
  456. .BR l
  457. (ell) qualifier is present, the input is a sequence of characters that
  458. begins in the initial shift state. Each character shall be converted to
  459. a wide character as if by a call to the
  460. \fImbrtowc\fR()
  461. function, with the conversion state described by an
  462. .BR mbstate_t
  463. object initialized to zero before the first character is converted.
  464. If the
  465. .BR 'm'
  466. assignment-allocation character is not specified, the application shall
  467. ensure that the corresponding argument is a pointer to an array of
  468. .BR wchar_t
  469. large enough to accept the sequence and the terminating null wide
  470. character, which shall be added automatically.
  471. Otherwise, the application shall ensure that the corresponding
  472. argument is a pointer to a pointer to a
  473. .BR wchar_t .
  474. .RE
  475. .IP "\fR[\fR" 8
  476. Matches a non-empty sequence of bytes from a set of expected bytes (the
  477. .IR scanset ).
  478. The normal skip over white-space characters shall be suppressed in this
  479. case. If the
  480. .BR 'm'
  481. assignment-allocation character is not specified, the application shall
  482. ensure that the corresponding argument is a pointer to the initial byte
  483. of an array of
  484. .BR char ,
  485. .BR "signed char" ,
  486. or
  487. .BR "unsigned char"
  488. large enough to accept the sequence and a terminating null byte, which
  489. shall be added automatically.
  490. Otherwise, the application shall ensure that the corresponding
  491. argument is a pointer to a pointer to a
  492. .BR char .
  493. .RS 8
  494. .P
  495. If an
  496. .BR l
  497. (ell) qualifier is present, the input is a sequence of characters that
  498. begins in the initial shift state. Each character in the sequence shall
  499. be converted to a wide character as if by a call to the
  500. \fImbrtowc\fR()
  501. function, with the conversion state described by an
  502. .BR mbstate_t
  503. object initialized to zero before the first character is converted.
  504. If the
  505. .BR 'm'
  506. assignment-allocation character is not specified, the application shall
  507. ensure that the corresponding argument is a pointer to an array of
  508. .BR wchar_t
  509. large enough to accept the sequence and the terminating null wide
  510. character, which shall be added automatically.
  511. .br
  512. Otherwise, the application shall ensure that the corresponding
  513. argument is a pointer to a pointer to a
  514. .BR wchar_t .
  515. .P
  516. The conversion specification includes all subsequent bytes in the
  517. .IR format
  518. string up to and including the matching
  519. <right-square-bracket>
  520. (\c
  521. .BR ']' ).
  522. The bytes between the square brackets (the
  523. .IR scanlist )
  524. comprise the scanset, unless the byte after the
  525. <left-square-bracket>
  526. is a
  527. <circumflex>
  528. (\c
  529. .BR '\(ha' ),
  530. in which case the scanset contains all bytes that do not appear in the
  531. scanlist between the
  532. <circumflex>
  533. and the
  534. <right-square-bracket>.
  535. If the conversion specification begins with
  536. .BR \(dq[\|]\(dq
  537. or
  538. .BR \(dq[\(ha]\(dq ,
  539. the
  540. <right-square-bracket>
  541. is included in the scanlist and the next
  542. <right-square-bracket>
  543. is the matching
  544. <right-square-bracket>
  545. that ends the conversion specification; otherwise, the first
  546. <right-square-bracket>
  547. is the one that ends the conversion specification. If a
  548. .BR '\-'
  549. is in the scanlist and is not the first character, nor the second where
  550. the first character is a
  551. .BR '\(ha' ,
  552. nor the last character, the behavior is implementation-defined.
  553. .RE
  554. .IP "\fRc\fP" 8
  555. Matches a sequence of bytes of the number specified by the field width
  556. (1 if no field width is present in the conversion specification). No
  557. null byte is added. The normal skip over white-space characters
  558. shall be suppressed in this case. If the
  559. .BR 'm'
  560. assignment-allocation character is not specified, the application shall
  561. ensure that the corresponding argument is a pointer to the initial byte
  562. of an array of
  563. .BR char ,
  564. .BR "signed char" ,
  565. or
  566. .BR "unsigned char"
  567. large enough to accept the sequence.
  568. Otherwise, the application shall ensure that the corresponding
  569. argument is a pointer to a pointer to a
  570. .BR char .
  571. .RS 8
  572. .P
  573. If an
  574. .BR l
  575. (ell) qualifier is present, the input shall be a sequence of characters
  576. that begins in the initial shift state. Each character in the sequence
  577. is converted to a wide character as if by a call to the
  578. \fImbrtowc\fR()
  579. function, with the conversion state described by an
  580. .BR mbstate_t
  581. object initialized to zero before the first character is converted.
  582. No null wide character is added. If the
  583. .BR 'm'
  584. assignment-allocation character is not specified, the application shall
  585. ensure that the corresponding argument is a pointer to an array of
  586. .BR wchar_t
  587. large enough to accept the resulting sequence of wide characters.
  588. Otherwise, the application shall ensure that the corresponding
  589. argument is a pointer to a pointer to a
  590. .BR wchar_t .
  591. .RE
  592. .IP "\fRp\fP" 8
  593. Matches an implementation-defined set of sequences, which shall be the
  594. same as the set of sequences that is produced by the
  595. .BR %p
  596. conversion specification of the corresponding
  597. \fIfprintf\fR()
  598. functions. The application shall ensure that the corresponding argument
  599. is a pointer to a pointer to
  600. .BR void .
  601. The interpretation of the input item is implementation-defined. If
  602. the input item is a value converted earlier during the same program
  603. execution, the pointer that results shall compare equal to that value;
  604. otherwise, the behavior of the
  605. .BR %p
  606. conversion specification is undefined.
  607. .IP "\fRn\fP" 8
  608. No input is consumed. The application shall ensure that the
  609. corresponding argument is a pointer to the integer into which shall be
  610. written the number of bytes read from the input so far by this call to
  611. the
  612. \fIfscanf\fR()
  613. functions. Execution of a
  614. .BR %n
  615. conversion specification shall not increment the assignment count
  616. returned at the completion of execution of the function. No argument
  617. shall be converted, but one shall be consumed. If the conversion
  618. specification includes an assignment-suppressing character or a field
  619. width, the behavior is undefined.
  620. .IP "\fRC\fP" 8
  621. Equivalent to
  622. .BR lc .
  623. .IP "\fRS\fP" 8
  624. Equivalent to
  625. .BR ls .
  626. .IP "\fR%\fR" 8
  627. Matches a single
  628. .BR '%'
  629. character; no conversion or assignment occurs. The complete conversion
  630. specification shall be
  631. .BR %% .
  632. .P
  633. If a conversion specification is invalid, the behavior is undefined.
  634. .P
  635. The conversion specifiers
  636. .BR A ,
  637. .BR E ,
  638. .BR F ,
  639. .BR G ,
  640. and
  641. .BR X
  642. are also valid and shall be equivalent to
  643. .BR a ,
  644. .BR e ,
  645. .BR f ,
  646. .BR g ,
  647. and
  648. .BR x ,
  649. respectively.
  650. .P
  651. If end-of-file is encountered during input, conversion shall be
  652. terminated. If end-of-file occurs before any bytes matching the current
  653. conversion specification (except for
  654. .BR %n )
  655. have been read (other than leading white-space characters, where
  656. permitted), execution of the current conversion specification shall
  657. terminate with an input failure. Otherwise, unless execution of the
  658. current conversion specification is terminated with a matching failure,
  659. execution of the following conversion specification (if any) shall be
  660. terminated with an input failure.
  661. .P
  662. Reaching the end of the string in
  663. \fIsscanf\fR()
  664. shall be equivalent to encountering end-of-file for
  665. \fIfscanf\fR().
  666. .P
  667. If conversion terminates on a conflicting input, the offending input is
  668. left unread in the input. Any trailing white space (including
  669. <newline>
  670. characters) shall be left unread unless matched by a conversion
  671. specification. The success of literal matches and suppressed assignments
  672. is only directly determinable via the
  673. .BR %n
  674. conversion specification.
  675. .P
  676. The
  677. \fIfscanf\fR()
  678. and
  679. \fIscanf\fR()
  680. functions may mark the last data access timestamp of the file
  681. associated with
  682. .IR stream
  683. for update. The last data access timestamp shall be
  684. marked for update by the first successful execution of
  685. \fIfgetc\fR(),
  686. \fIfgets\fR(),
  687. \fIfread\fR(),
  688. \fIgetc\fR(),
  689. \fIgetchar\fR(),
  690. \fIgetdelim\fR(),
  691. \fIgetline\fR(),
  692. \fIgets\fR(),
  693. \fIfscanf\fR(),
  694. or
  695. \fIscanf\fR()
  696. using
  697. .IR stream
  698. that returns data not supplied by a prior call to
  699. \fIungetc\fR().
  700. .SH "RETURN VALUE"
  701. Upon successful completion, these functions shall return the number of
  702. successfully matched and assigned input items; this number can be zero
  703. in the event of an early matching failure. If the input ends before the
  704. first conversion (if any) has completed, and without a matching failure
  705. having occurred, EOF shall be returned. If an error occurs before the
  706. first conversion (if any) has completed, and without a matching failure
  707. having occurred, EOF shall be returned
  708. and
  709. .IR errno
  710. shall be set to indicate the error.
  711. If a read error occurs, the error indicator for the stream shall be set.
  712. .SH ERRORS
  713. For the conditions under which the
  714. \fIfscanf\fR()
  715. functions fail and may fail, refer to
  716. .IR "\fIfgetc\fR\^(\|)"
  717. or
  718. .IR "\fIfgetwc\fR\^(\|)".
  719. .P
  720. In addition, the
  721. \fIfscanf\fR()
  722. function shall fail if:
  723. .TP
  724. .BR EILSEQ
  725. Input byte sequence does not form a valid character.
  726. .TP
  727. .BR ENOMEM
  728. Insufficient storage space is available.
  729. .P
  730. In addition, the
  731. \fIfscanf\fR()
  732. function may fail if:
  733. .TP
  734. .BR EINVAL
  735. There are insufficient arguments.
  736. .LP
  737. .IR "The following sections are informative."
  738. .SH "EXAMPLES"
  739. The call:
  740. .sp
  741. .RS 4
  742. .nf
  743. int i, n; float x; char name[50];
  744. n = scanf("%d%f%s", &i, &x, name);
  745. .fi
  746. .P
  747. .RE
  748. .P
  749. with the input line:
  750. .sp
  751. .RS 4
  752. .nf
  753. 25 54.32E-1 Hamster
  754. .fi
  755. .P
  756. .RE
  757. .P
  758. assigns to
  759. .IR n
  760. the value 3, to
  761. .IR i
  762. the value 25, to
  763. .IR x
  764. the value 5.432, and
  765. .IR name
  766. contains the string
  767. .BR \(dqHamster\(dq .
  768. .P
  769. The call:
  770. .sp
  771. .RS 4
  772. .nf
  773. int i; float x; char name[50];
  774. (void) scanf("%2d%f%*d %[0123456789]", &i, &x, name);
  775. .fi
  776. .P
  777. .RE
  778. .P
  779. with input:
  780. .sp
  781. .RS 4
  782. .nf
  783. 56789 0123 56a72
  784. .fi
  785. .P
  786. .RE
  787. .P
  788. assigns 56 to
  789. .IR i ,
  790. 789.0 to
  791. .IR x ,
  792. skips 0123, and places the string
  793. .BR \(dq56\e0\(dq
  794. in
  795. .IR name .
  796. The next call to
  797. \fIgetchar\fR()
  798. shall return the character
  799. .BR 'a' .
  800. .SS "Reading Data into an Array"
  801. .P
  802. The following call uses
  803. \fIfscanf\fR()
  804. to read three floating-point numbers from standard input into the
  805. .IR input
  806. array.
  807. .sp
  808. .RS 4
  809. .nf
  810. float input[3]; fscanf (stdin, "%f %f %f", input, input+1, input+2);
  811. .fi
  812. .P
  813. .RE
  814. .SH "APPLICATION USAGE"
  815. If the application calling
  816. \fIfscanf\fR()
  817. has any objects of type
  818. .BR wint_t
  819. or
  820. .BR wchar_t ,
  821. it must also include the
  822. .IR <wchar.h>
  823. header to have these objects defined.
  824. .P
  825. For functions that allocate memory as if by
  826. \fImalloc\fR(),
  827. the application should release such memory when it is no longer
  828. required by a call to
  829. \fIfree\fR().
  830. For
  831. \fIfscanf\fR(),
  832. this is memory allocated via use of the
  833. .BR 'm'
  834. assignment-allocation character.
  835. .SH RATIONALE
  836. This function is aligned with the ISO/IEC\ 9899:\|1999 standard, and in doing so a few
  837. ``obvious'' things were not included. Specifically, the set of
  838. characters allowed in a scanset is limited to single-byte characters.
  839. In other similar places, multi-byte characters have been permitted, but
  840. for alignment with the ISO/IEC\ 9899:\|1999 standard, it has not been done here. Applications
  841. needing this could use the corresponding wide-character functions to
  842. achieve the desired results.
  843. .SH "FUTURE DIRECTIONS"
  844. None.
  845. .SH "SEE ALSO"
  846. .IR "Section 2.5" ", " "Standard I/O Streams",
  847. .IR "\fIfprintf\fR\^(\|)",
  848. .IR "\fIgetc\fR\^(\|)",
  849. .IR "\fIsetlocale\fR\^(\|)",
  850. .IR "\fIstrtod\fR\^(\|)",
  851. .IR "\fIstrtol\fR\^(\|)",
  852. .IR "\fIstrtoul\fR\^(\|)",
  853. .IR "\fIwcrtomb\fR\^(\|)"
  854. .P
  855. The Base Definitions volume of POSIX.1\(hy2017,
  856. .IR "Chapter 7" ", " "Locale",
  857. .IR "\fB<inttypes.h>\fP",
  858. .IR "\fB<langinfo.h>\fP",
  859. .IR "\fB<stdio.h>\fP",
  860. .IR "\fB<wchar.h>\fP"
  861. .\"
  862. .SH COPYRIGHT
  863. Portions of this text are reprinted and reproduced in electronic form
  864. from IEEE Std 1003.1-2017, Standard for Information Technology
  865. -- Portable Operating System Interface (POSIX), The Open Group Base
  866. Specifications Issue 7, 2018 Edition,
  867. Copyright (C) 2018 by the Institute of
  868. Electrical and Electronics Engineers, Inc and The Open Group.
  869. In the event of any discrepancy between this version and the original IEEE and
  870. The Open Group Standard, the original IEEE and The Open Group Standard
  871. is the referee document. The original Standard can be obtained online at
  872. http://www.opengroup.org/unix/online.html .
  873. .PP
  874. Any typographical or formatting errors that appear
  875. in this page are most likely
  876. to have been introduced during the conversion of the source files to
  877. man page format. To report such errors, see
  878. https://www.kernel.org/doc/man-pages/reporting_bugs.html .