You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 3 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <title>swscanf (The Red Hat newlib C Library)</title>
  7. <meta name="description" content="swscanf (The Red Hat newlib C Library)">
  8. <meta name="keywords" content="swscanf (The Red Hat newlib C Library)">
  9. <meta name="resource-type" content="document">
  10. <meta name="distribution" content="global">
  11. <meta name="Generator" content="makeinfo">
  12. <link href="index.html#Top" rel="start" title="Top">
  13. <link href="Document-Index.html#Document-Index" rel="index" title="Document Index">
  14. <link href="Document-Index.html#SEC_Contents" rel="contents" title="Table of Contents">
  15. <link href="Stdio.html#Stdio" rel="up" title="Stdio">
  16. <link href="tmpfile.html#tmpfile" rel="next" title="tmpfile">
  17. <link href="swprintf.html#swprintf" rel="prev" title="swprintf">
  18. <style type="text/css">
  19. <!--
  20. a.summary-letter {text-decoration: none}
  21. blockquote.indentedblock {margin-right: 0em}
  22. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  23. blockquote.smallquotation {font-size: smaller}
  24. div.display {margin-left: 3.2em}
  25. div.example {margin-left: 3.2em}
  26. div.lisp {margin-left: 3.2em}
  27. div.smalldisplay {margin-left: 3.2em}
  28. div.smallexample {margin-left: 3.2em}
  29. div.smalllisp {margin-left: 3.2em}
  30. kbd {font-style: oblique}
  31. pre.display {font-family: inherit}
  32. pre.format {font-family: inherit}
  33. pre.menu-comment {font-family: serif}
  34. pre.menu-preformatted {font-family: serif}
  35. pre.smalldisplay {font-family: inherit; font-size: smaller}
  36. pre.smallexample {font-size: smaller}
  37. pre.smallformat {font-family: inherit; font-size: smaller}
  38. pre.smalllisp {font-size: smaller}
  39. span.nolinebreak {white-space: nowrap}
  40. span.roman {font-family: initial; font-weight: normal}
  41. span.sansserif {font-family: sans-serif; font-weight: normal}
  42. ul.no-bullet {list-style: none}
  43. -->
  44. </style>
  45. </head>
  46. <body lang="en">
  47. <a name="swscanf"></a>
  48. <div class="header">
  49. <p>
  50. Next: <a href="tmpfile.html#tmpfile" accesskey="n" rel="next">tmpfile</a>, Previous: <a href="swprintf.html#swprintf" accesskey="p" rel="prev">swprintf</a>, Up: <a href="Stdio.html#Stdio" accesskey="u" rel="up">Stdio</a> &nbsp; [<a href="Document-Index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Document-Index.html#Document-Index" title="Index" rel="index">Index</a>]</p>
  51. </div>
  52. <hr>
  53. <a name="swscanf_002c-fwscanf_002c-wscanf_002d_002d_002dscan-and-format-wide-character-input"></a>
  54. <h3 class="section">4.65 <code>swscanf</code>, <code>fwscanf</code>, <code>wscanf</code>&mdash;scan and format wide character input</h3>
  55. <a name="index-wscanf"></a>
  56. <a name="index-_005fwscanf_005fr"></a>
  57. <a name="index-fwscanf"></a>
  58. <a name="index-_005ffwscanf_005fr"></a>
  59. <a name="index-swscanf"></a>
  60. <a name="index-_005fswscanf_005fr"></a>
  61. <p><strong>Synopsis</strong>
  62. </p><div class="example">
  63. <pre class="example">#include &lt;stdio.h&gt;
  64. int wscanf(const wchar_t *__restrict <var>format</var>, ...);
  65. int fwscanf(FILE *__restrict <var>fd</var>,
  66. const wchar_t *__restrict <var>format</var>, ...);
  67. int swscanf(const wchar_t *__restrict <var>str</var>,
  68. const wchar_t *__restrict <var>format</var>, ...);
  69. int _wscanf_r(struct _reent *<var>ptr</var>, const wchar_t *<var>format</var>, ...);
  70. int _fwscanf_r(struct _reent *<var>ptr</var>, FILE *<var>fd</var>,
  71. const wchar_t *<var>format</var>, ...);
  72. int _swscanf_r(struct _reent *<var>ptr</var>, const wchar_t *<var>str</var>,
  73. const wchar_t *<var>format</var>, ...);
  74. </pre></div>
  75. <p><strong>Description</strong><br>
  76. <code>wscanf</code> scans a series of input fields from standard input,
  77. one wide character at a time. Each field is interpreted according to
  78. a format specifier passed to <code>wscanf</code> in the format string at
  79. <code>*<var>format</var></code>. <code>wscanf</code> stores the interpreted input from
  80. each field at the address passed to it as the corresponding argument
  81. following <var>format</var>. You must supply the same number of
  82. format specifiers and address arguments as there are input fields.
  83. </p>
  84. <p>There must be sufficient address arguments for the given format
  85. specifiers; if not the results are unpredictable and likely
  86. disasterous. Excess address arguments are merely ignored.
  87. </p>
  88. <p><code>wscanf</code> often produces unexpected results if the input diverges from
  89. an expected pattern. Since the combination of <code>gets</code> or <code>fgets</code>
  90. followed by <code>swscanf</code> is safe and easy, that is the preferred way
  91. to be certain that a program is synchronized with input at the end
  92. of a line.
  93. </p>
  94. <p><code>fwscanf</code> and <code>swscanf</code> are identical to <code>wscanf</code>, other than the
  95. source of input: <code>fwscanf</code> reads from a file, and <code>swscanf</code>
  96. from a string.
  97. </p>
  98. <p>The routines <code>_wscanf_r</code>, <code>_fwscanf_r</code>, and <code>_swscanf_r</code> are reentrant
  99. versions of <code>wscanf</code>, <code>fwscanf</code>, and <code>swscanf</code> that take an additional
  100. first argument pointing to a reentrancy structure.
  101. </p>
  102. <p>The string at <code>*<var>format</var></code> is a wide character sequence composed
  103. of zero or more directives. Directives are composed of
  104. one or more whitespace characters, non-whitespace characters,
  105. and format specifications.
  106. </p>
  107. <p>Whitespace characters are blank (<code> </code>), tab (<code>\t</code>), or
  108. newline (<code>\n</code>).
  109. When <code>wscanf</code> encounters a whitespace character in the format string
  110. it will read (but not store) all consecutive whitespace characters
  111. up to the next non-whitespace character in the input.
  112. </p>
  113. <p>Non-whitespace characters are all other ASCII characters except the
  114. percent sign (<code>%</code>). When <code>wscanf</code> encounters a non-whitespace
  115. character in the format string it will read, but not store
  116. a matching non-whitespace character.
  117. </p>
  118. <p>Format specifications tell <code>wscanf</code> to read and convert characters
  119. from the input field into specific types of values, and store then
  120. in the locations specified by the address arguments.
  121. </p>
  122. <p>Trailing whitespace is left unread unless explicitly
  123. matched in the format string.
  124. </p>
  125. <p>The format specifiers must begin with a percent sign (<code>%</code>)
  126. and have the following form:
  127. </p>
  128. <div class="smallexample">
  129. <pre class="smallexample"> %[*][<var>width</var>][<var>size</var>]<var>type</var>
  130. </pre></div>
  131. <p>Each format specification begins with the percent character (<code>%</code>).
  132. The other fields are:
  133. </p><ul>
  134. <li> *
  135. <p>an optional marker; if present, it suppresses interpretation and
  136. assignment of this input field.
  137. </p>
  138. </li><li> <var>width</var>
  139. <p>an optional maximum field width: a decimal integer,
  140. which controls the maximum number of characters that
  141. will be read before converting the current input field. If the
  142. input field has fewer than <var>width</var> characters, <code>wscanf</code>
  143. reads all the characters in the field, and then
  144. proceeds with the next field and its format specification.
  145. </p>
  146. <p>If a whitespace or a non-convertable wide character occurs
  147. before <var>width</var> character are read, the characters up
  148. to that character are read, converted, and stored.
  149. Then <code>wscanf</code> proceeds to the next format specification.
  150. </p>
  151. </li><li> <var>size</var>
  152. <p><code>h</code>, <code>j</code>, <code>l</code>, <code>L</code>, <code>t</code>, and <code>z</code> are optional size
  153. characters which override the default way that <code>wscanf</code>
  154. interprets the data type of the corresponding argument.
  155. </p>
  156. <table>
  157. <thead><tr><th width="18%">Modifier</th><th width="30%">Type(s)</th><th width="52%"></th></tr></thead>
  158. <tr><td width="18%">hh</td><td width="30%">d, i, o, u, x, n</td><td width="52%">convert input to char, store in char object</td></tr>
  159. <tr><td width="18%">h</td><td width="30%">d, i, o, u, x, n</td><td width="52%">convert input to short, store in short object</td></tr>
  160. <tr><td width="18%">h</td><td width="30%">e, f, c, s, p</td><td width="52%">no effect</td></tr>
  161. <tr><td width="18%">j</td><td width="30%">d, i, o, u, x, n</td><td width="52%">convert input to intmax_t, store in intmax_t object</td></tr>
  162. <tr><td width="18%">j</td><td width="30%">all others</td><td width="52%">no effect</td></tr>
  163. <tr><td width="18%">l</td><td width="30%">d, i, o, u, x, n</td><td width="52%">convert input to long, store in long object</td></tr>
  164. <tr><td width="18%">l</td><td width="30%">e, f, g</td><td width="52%">convert input to double, store in a double object</td></tr>
  165. <tr><td width="18%">l</td><td width="30%">c, s, [</td><td width="52%">the input is stored in a wchar_t object</td></tr>
  166. <tr><td width="18%">l</td><td width="30%">p</td><td width="52%">no effect</td></tr>
  167. <tr><td width="18%">ll</td><td width="30%">d, i, o, u, x, n</td><td width="52%">convert to long long, store in long long object</td></tr>
  168. <tr><td width="18%">L</td><td width="30%">d, i, o, u, x, n</td><td width="52%">convert to long long, store in long long object</td></tr>
  169. <tr><td width="18%">L</td><td width="30%">e, f, g, E, G</td><td width="52%">convert to long double, store in long double object</td></tr>
  170. <tr><td width="18%">L</td><td width="30%">all others</td><td width="52%">no effect</td></tr>
  171. <tr><td width="18%">t</td><td width="30%">d, i, o, u, x, n</td><td width="52%">convert input to ptrdiff_t, store in ptrdiff_t object</td></tr>
  172. <tr><td width="18%">t</td><td width="30%">all others</td><td width="52%">no effect</td></tr>
  173. <tr><td width="18%">z</td><td width="30%">d, i, o, u, x, n</td><td width="52%">convert input to size_t, store in size_t object</td></tr>
  174. <tr><td width="18%">z</td><td width="30%">all others</td><td width="52%">no effect</td></tr>
  175. </table>
  176. </li><li> <var>type</var>
  177. <p>A character to specify what kind of conversion
  178. <code>wscanf</code> performs. Here is a table of the conversion
  179. characters:
  180. </p>
  181. <dl compact="compact">
  182. <dt><code>%</code></dt>
  183. <dd><p>No conversion is done; the percent character (<code>%</code>) is stored.
  184. </p>
  185. </dd>
  186. <dt><code>c</code></dt>
  187. <dd><p>Scans one wide character. Corresponding <var>arg</var>: <code>(char *arg)</code>.
  188. Otherwise, if an <code>l</code> specifier is present, the corresponding
  189. <var>arg</var> is a <code>(wchar_t *arg)</code>.
  190. </p>
  191. </dd>
  192. <dt><code>s</code></dt>
  193. <dd><p>Reads a character string into the array supplied.
  194. Corresponding <var>arg</var>: <code>(char arg[])</code>.
  195. If an <code>l</code> specifier is present, the corresponding <var>arg</var> is a <code>(wchar_t *arg)</code>.
  196. </p>
  197. </dd>
  198. <dt><code>[<var>pattern</var>]</code></dt>
  199. <dd><p>Reads a non-empty character string into memory
  200. starting at <var>arg</var>. This area must be large
  201. enough to accept the sequence and a
  202. terminating null character which will be added
  203. automatically. (<var>pattern</var> is discussed in the paragraph following
  204. this table). Corresponding <var>arg</var>: <code>(char *arg)</code>.
  205. If an <code>l</code> specifier is present, the corresponding <var>arg</var> is
  206. a <code>(wchar_t *arg)</code>.
  207. </p>
  208. </dd>
  209. <dt><code>d</code></dt>
  210. <dd><p>Reads a decimal integer into the corresponding <var>arg</var>: <code>(int *arg)</code>.
  211. </p>
  212. </dd>
  213. <dt><code>o</code></dt>
  214. <dd><p>Reads an octal integer into the corresponding <var>arg</var>: <code>(int *arg)</code>.
  215. </p>
  216. </dd>
  217. <dt><code>u</code></dt>
  218. <dd><p>Reads an unsigned decimal integer into the corresponding
  219. <var>arg</var>: <code>(unsigned int *arg)</code>.
  220. </p>
  221. </dd>
  222. <dt><code>x,X</code></dt>
  223. <dd><p>Read a hexadecimal integer into the corresponding <var>arg</var>:
  224. <code>(int *arg)</code>.
  225. </p>
  226. </dd>
  227. <dt><code>e, f, g</code></dt>
  228. <dd><p>Read a floating-point number into the corresponding <var>arg</var>:
  229. <code>(float *arg)</code>.
  230. </p>
  231. </dd>
  232. <dt><code>E, F, G</code></dt>
  233. <dd><p>Read a floating-point number into the corresponding <var>arg</var>:
  234. <code>(double *arg)</code>.
  235. </p>
  236. </dd>
  237. <dt><code>i</code></dt>
  238. <dd><p>Reads a decimal, octal or hexadecimal integer into the
  239. corresponding <var>arg</var>: <code>(int *arg)</code>.
  240. </p>
  241. </dd>
  242. <dt><code>n</code></dt>
  243. <dd><p>Stores the number of characters read in the corresponding
  244. <var>arg</var>: <code>(int *arg)</code>.
  245. </p>
  246. </dd>
  247. <dt><code>p</code></dt>
  248. <dd><p>Stores a scanned pointer. ANSI C leaves the details
  249. to each implementation; this implementation treats
  250. <code>%p</code> exactly the same as <code>%U</code>. Corresponding
  251. <var>arg</var>: <code>(void **arg)</code>.
  252. </p></dd>
  253. </dl>
  254. <p>A <var>pattern</var> of characters surrounded by square brackets can be used
  255. instead of the <code>s</code> type character. <var>pattern</var> is a set of
  256. characters which define a search set of possible characters making up
  257. the <code>wscanf</code> input field. If the first character in the brackets is a
  258. caret (<code>^</code>), the search set is inverted to include all ASCII characters
  259. except those between the brackets. There is no range facility as is
  260. defined in the corresponding non-wide character scanf functions.
  261. Ranges are not part of the POSIX standard.
  262. </p>
  263. <p>Here are some <var>pattern</var> examples:
  264. </p><dl compact="compact">
  265. <dt><code>%[abcd]</code></dt>
  266. <dd><p>matches wide character strings containing only
  267. <code>a</code>, <code>b</code>, <code>c</code>, and <code>d</code>.
  268. </p>
  269. </dd>
  270. <dt><code>%[^abcd]</code></dt>
  271. <dd><p>matches wide character strings containing any characters except
  272. <code>a</code>, <code>b</code>, <code>c</code>, or <code>d</code>.
  273. </p>
  274. </dd>
  275. <dt><code>%[A-DW-Z]</code></dt>
  276. <dd><p>Note: No wide character ranges, so this expression matches wide
  277. character strings containing <code>A</code>, <code>-</code>, <code>D</code>, <code>W</code>, <code>Z</code>.
  278. </p></dd>
  279. </dl>
  280. <p>Floating point numbers (for field types <code>e</code>, <code>f</code>, <code>g</code>, <code>E</code>,
  281. <code>F</code>, <code>G</code>) must correspond to the following general form:
  282. </p>
  283. <div class="smallexample">
  284. <pre class="smallexample"> [+/-] ddddd[.]ddd [E|e[+|-]ddd]
  285. </pre></div>
  286. <p>where objects inclosed in square brackets are optional, and <code>ddd</code>
  287. represents decimal, octal, or hexadecimal digits.
  288. </p></li></ul>
  289. <br>
  290. <p><strong>Returns</strong><br>
  291. <code>wscanf</code> returns the number of input fields successfully
  292. scanned, converted and stored; the return value does
  293. not include scanned fields which were not stored.
  294. </p>
  295. <p>If <code>wscanf</code> attempts to read at end-of-file, the return
  296. value is <code>EOF</code>.
  297. </p>
  298. <p>If no fields were stored, the return value is <code>0</code>.
  299. </p>
  300. <p><code>wscanf</code> might stop scanning a particular field before
  301. reaching the normal field end character, or may
  302. terminate entirely.
  303. </p>
  304. <p><code>wscanf</code> stops scanning and storing the current field
  305. and moves to the next input field (if any)
  306. in any of the following situations:
  307. </p>
  308. <ul>
  309. <li> The assignment suppressing character (<code>*</code>) appears
  310. after the <code>%</code> in the format specification; the current
  311. input field is scanned but not stored.
  312. </li><li> <var>width</var> characters have been read (<var>width</var> is a
  313. width specification, a positive decimal integer).
  314. </li><li> The next wide character read cannot be converted
  315. under the the current format (for example,
  316. if a <code>Z</code> is read when the format is decimal).
  317. </li><li> The next wide character in the input field does not appear
  318. in the search set (or does appear in the inverted search set).
  319. </li></ul>
  320. <p>When <code>wscanf</code> stops scanning the current input field for one of
  321. these reasons, the next character is considered unread and
  322. used as the first character of the following input field, or the
  323. first character in a subsequent read operation on the input.
  324. </p>
  325. <p><code>wscanf</code> will terminate under the following circumstances:
  326. </p>
  327. <ul>
  328. <li> The next wide character in the input field conflicts
  329. with a corresponding non-whitespace character in the
  330. format string.
  331. </li><li> The next wide character in the input field is <code>WEOF</code>.
  332. </li><li> The format string has been exhausted.
  333. </li></ul>
  334. <p>When the format string contains a wide character sequence that is
  335. not part of a format specification, the same wide character
  336. sequence must appear in the input; <code>wscanf</code> will
  337. scan but not store the matched characters. If a
  338. conflict occurs, the first conflicting wide character remains in the
  339. input as if it had never been read.
  340. </p>
  341. <br>
  342. <p><strong>Portability</strong><br>
  343. <code>wscanf</code> is C99, POSIX-1.2008.
  344. </p>
  345. <p>Supporting OS subroutines required: <code>close</code>, <code>fstat</code>, <code>isatty</code>,
  346. <code>lseek</code>, <code>read</code>, <code>sbrk</code>, <code>write</code>.
  347. </p>
  348. <br>
  349. <hr>
  350. <div class="header">
  351. <p>
  352. Next: <a href="tmpfile.html#tmpfile" accesskey="n" rel="next">tmpfile</a>, Previous: <a href="swprintf.html#swprintf" accesskey="p" rel="prev">swprintf</a>, Up: <a href="Stdio.html#Stdio" accesskey="u" rel="up">Stdio</a> &nbsp; [<a href="Document-Index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Document-Index.html#Document-Index" title="Index" rel="index">Index</a>]</p>
  353. </div>
  354. </body>
  355. </html>