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.

sscanf.html 17KB

3 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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>sscanf (The Red Hat newlib C Library)</title>
  7. <meta name="description" content="sscanf (The Red Hat newlib C Library)">
  8. <meta name="keywords" content="sscanf (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="stdio_005fext.html#stdio_005fext" rel="next" title="stdio_ext">
  17. <link href="sprintf.html#sprintf" rel="prev" title="sprintf">
  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="sscanf"></a>
  48. <div class="header">
  49. <p>
  50. Next: <a href="stdio_005fext.html#stdio_005fext" accesskey="n" rel="next">stdio_ext</a>, Previous: <a href="sprintf.html#sprintf" accesskey="p" rel="prev">sprintf</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="sscanf_002c-fscanf_002c-scanf_002d_002d_002dscan-and-format-input"></a>
  54. <h3 class="section">4.62 <code>sscanf</code>, <code>fscanf</code>, <code>scanf</code>&mdash;scan and format input</h3>
  55. <a name="index-scanf"></a>
  56. <a name="index-_005fscanf_005fr"></a>
  57. <a name="index-fscanf"></a>
  58. <a name="index-_005ffscanf_005fr"></a>
  59. <a name="index-sscanf"></a>
  60. <a name="index-_005fsscanf_005fr"></a>
  61. <p><strong>Synopsis</strong>
  62. </p><div class="example">
  63. <pre class="example">#include &lt;stdio.h&gt;
  64. int scanf(const char *restrict <var>format</var>, ...);
  65. int fscanf(FILE *restrict <var>fd</var>, const char *restrict <var>format</var>, ...);
  66. int sscanf(const char *restrict <var>str</var>, const char *restrict <var>format</var>, ...);
  67. int _scanf_r(struct _reent *<var>ptr</var>, const char *restrict <var>format</var>, ...);
  68. int _fscanf_r(struct _reent *<var>ptr</var>, FILE *restrict <var>fd</var>,
  69. const char *restrict <var>format</var>, ...);
  70. int _sscanf_r(struct _reent *<var>ptr</var>, const char *restrict <var>str</var>,
  71. const char *restrict <var>format</var>, ...);
  72. </pre></div>
  73. <p><strong>Description</strong><br>
  74. <code>scanf</code> scans a series of input fields from standard input,
  75. one character at a time. Each field is interpreted according to
  76. a format specifier passed to <code>scanf</code> in the format string at
  77. <code>*<var>format</var></code>. <code>scanf</code> stores the interpreted input from
  78. each field at the address passed to it as the corresponding argument
  79. following <var>format</var>. You must supply the same number of
  80. format specifiers and address arguments as there are input fields.
  81. </p>
  82. <p>There must be sufficient address arguments for the given format
  83. specifiers; if not the results are unpredictable and likely
  84. disasterous. Excess address arguments are merely ignored.
  85. </p>
  86. <p><code>scanf</code> often produces unexpected results if the input diverges from
  87. an expected pattern. Since the combination of <code>gets</code> or <code>fgets</code>
  88. followed by <code>sscanf</code> is safe and easy, that is the preferred way
  89. to be certain that a program is synchronized with input at the end
  90. of a line.
  91. </p>
  92. <p><code>fscanf</code> and <code>sscanf</code> are identical to <code>scanf</code>, other than the
  93. source of input: <code>fscanf</code> reads from a file, and <code>sscanf</code>
  94. from a string.
  95. </p>
  96. <p>The routines <code>_scanf_r</code>, <code>_fscanf_r</code>, and <code>_sscanf_r</code> are reentrant
  97. versions of <code>scanf</code>, <code>fscanf</code>, and <code>sscanf</code> that take an additional
  98. first argument pointing to a reentrancy structure.
  99. </p>
  100. <p>The string at <code>*<var>format</var></code> is a character sequence composed
  101. of zero or more directives. Directives are composed of
  102. one or more whitespace characters, non-whitespace characters,
  103. and format specifications.
  104. </p>
  105. <p>Whitespace characters are blank (<code> </code>), tab (<code>\t</code>), or
  106. newline (<code>\n</code>).
  107. When <code>scanf</code> encounters a whitespace character in the format string
  108. it will read (but not store) all consecutive whitespace characters
  109. up to the next non-whitespace character in the input.
  110. </p>
  111. <p>Non-whitespace characters are all other ASCII characters except the
  112. percent sign (<code>%</code>). When <code>scanf</code> encounters a non-whitespace
  113. character in the format string it will read, but not store
  114. a matching non-whitespace character.
  115. </p>
  116. <p>Format specifications tell <code>scanf</code> to read and convert characters
  117. from the input field into specific types of values, and store then
  118. in the locations specified by the address arguments.
  119. </p>
  120. <p>Trailing whitespace is left unread unless explicitly
  121. matched in the format string.
  122. </p>
  123. <p>The format specifiers must begin with a percent sign (<code>%</code>)
  124. and have the following form:
  125. </p>
  126. <div class="smallexample">
  127. <pre class="smallexample"> %[*][<var>width</var>][<var>size</var>]<var>type</var>
  128. </pre></div>
  129. <p>Each format specification begins with the percent character (<code>%</code>).
  130. The other fields are:
  131. </p><ul>
  132. <li> *
  133. <p>an optional marker; if present, it suppresses interpretation and
  134. assignment of this input field.
  135. </p>
  136. </li><li> <var>width</var>
  137. <p>an optional maximum field width: a decimal integer,
  138. which controls the maximum number of characters that
  139. will be read before converting the current input field. If the
  140. input field has fewer than <var>width</var> characters, <code>scanf</code>
  141. reads all the characters in the field, and then
  142. proceeds with the next field and its format specification.
  143. </p>
  144. <p>If a whitespace or a non-convertable character occurs
  145. before <var>width</var> character are read, the characters up
  146. to that character are read, converted, and stored.
  147. Then <code>scanf</code> proceeds to the next format specification.
  148. </p>
  149. </li><li> <var>size</var>
  150. <p><code>h</code>, <code>j</code>, <code>l</code>, <code>L</code>, <code>t</code>, and <code>z</code> are optional size
  151. characters which override the default way that <code>scanf</code>
  152. interprets the data type of the corresponding argument.
  153. </p>
  154. <table>
  155. <thead><tr><th width="18%">Modifier</th><th width="30%">Type(s)</th><th width="52%"></th></tr></thead>
  156. <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>
  157. <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>
  158. <tr><td width="18%">h</td><td width="30%">D, I, O, U, X, e, f, c, s, p</td><td width="52%">no effect</td></tr>
  159. <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>
  160. <tr><td width="18%">j</td><td width="30%">all others</td><td width="52%">no effect</td></tr>
  161. <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>
  162. <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>
  163. <tr><td width="18%">l</td><td width="30%">D, I, O, U, X, c, s, p</td><td width="52%">no effect</td></tr>
  164. <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>
  165. <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>
  166. <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>
  167. <tr><td width="18%">L</td><td width="30%">all others</td><td width="52%">no effect</td></tr>
  168. <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>
  169. <tr><td width="18%">t</td><td width="30%">all others</td><td width="52%">no effect</td></tr>
  170. <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>
  171. <tr><td width="18%">z</td><td width="30%">all others</td><td width="52%">no effect</td></tr>
  172. </table>
  173. </li><li> <var>type</var>
  174. <p>A character to specify what kind of conversion
  175. <code>scanf</code> performs. Here is a table of the conversion
  176. characters:
  177. </p>
  178. <dl compact="compact">
  179. <dt><code>%</code></dt>
  180. <dd><p>No conversion is done; the percent character (<code>%</code>) is stored.
  181. </p>
  182. </dd>
  183. <dt><code>c</code></dt>
  184. <dd><p>Scans one character. Corresponding <var>arg</var>: <code>(char *arg)</code>.
  185. </p>
  186. </dd>
  187. <dt><code>s</code></dt>
  188. <dd><p>Reads a character string into the array supplied.
  189. Corresponding <var>arg</var>: <code>(char arg[])</code>.
  190. </p>
  191. </dd>
  192. <dt><code>[<var>pattern</var>]</code></dt>
  193. <dd><p>Reads a non-empty character string into memory
  194. starting at <var>arg</var>. This area must be large
  195. enough to accept the sequence and a
  196. terminating null character which will be added
  197. automatically. (<var>pattern</var> is discussed in the paragraph following
  198. this table). Corresponding <var>arg</var>: <code>(char *arg)</code>.
  199. </p>
  200. </dd>
  201. <dt><code>d</code></dt>
  202. <dd><p>Reads a decimal integer into the corresponding <var>arg</var>: <code>(int *arg)</code>.
  203. </p>
  204. </dd>
  205. <dt><code>D</code></dt>
  206. <dd><p>Reads a decimal integer into the corresponding
  207. <var>arg</var>: <code>(long *arg)</code>.
  208. </p>
  209. </dd>
  210. <dt><code>o</code></dt>
  211. <dd><p>Reads an octal integer into the corresponding <var>arg</var>: <code>(int *arg)</code>.
  212. </p>
  213. </dd>
  214. <dt><code>O</code></dt>
  215. <dd><p>Reads an octal integer into the corresponding <var>arg</var>: <code>(long *arg)</code>.
  216. </p>
  217. </dd>
  218. <dt><code>u</code></dt>
  219. <dd><p>Reads an unsigned decimal integer into the corresponding
  220. <var>arg</var>: <code>(unsigned int *arg)</code>.
  221. </p>
  222. </dd>
  223. <dt><code>U</code></dt>
  224. <dd><p>Reads an unsigned decimal integer into the corresponding <var>arg</var>:
  225. <code>(unsigned long *arg)</code>.
  226. </p>
  227. </dd>
  228. <dt><code>x,X</code></dt>
  229. <dd><p>Read a hexadecimal integer into the corresponding <var>arg</var>:
  230. <code>(int *arg)</code>.
  231. </p>
  232. </dd>
  233. <dt><code>e, f, g</code></dt>
  234. <dd><p>Read a floating-point number into the corresponding <var>arg</var>:
  235. <code>(float *arg)</code>.
  236. </p>
  237. </dd>
  238. <dt><code>E, F, G</code></dt>
  239. <dd><p>Read a floating-point number into the corresponding <var>arg</var>:
  240. <code>(double *arg)</code>.
  241. </p>
  242. </dd>
  243. <dt><code>i</code></dt>
  244. <dd><p>Reads a decimal, octal or hexadecimal integer into the
  245. corresponding <var>arg</var>: <code>(int *arg)</code>.
  246. </p>
  247. </dd>
  248. <dt><code>I</code></dt>
  249. <dd><p>Reads a decimal, octal or hexadecimal integer into the
  250. corresponding <var>arg</var>: <code>(long *arg)</code>.
  251. </p>
  252. </dd>
  253. <dt><code>n</code></dt>
  254. <dd><p>Stores the number of characters read in the corresponding
  255. <var>arg</var>: <code>(int *arg)</code>.
  256. </p>
  257. </dd>
  258. <dt><code>p</code></dt>
  259. <dd><p>Stores a scanned pointer. ANSI C leaves the details
  260. to each implementation; this implementation treats
  261. <code>%p</code> exactly the same as <code>%U</code>. Corresponding
  262. <var>arg</var>: <code>(void **arg)</code>.
  263. </p></dd>
  264. </dl>
  265. <p>A <var>pattern</var> of characters surrounded by square brackets can be used
  266. instead of the <code>s</code> type character. <var>pattern</var> is a set of
  267. characters which define a search set of possible characters making up
  268. the <code>scanf</code> input field. If the first character in the brackets is a
  269. caret (<code>^</code>), the search set is inverted to include all ASCII characters
  270. except those between the brackets. There is also a range facility
  271. which you can use as a shortcut. <code>%[0-9] </code> matches all decimal digits.
  272. The hyphen must not be the first or last character in the set.
  273. The character prior to the hyphen must be lexically less than the
  274. character after it.
  275. </p>
  276. <p>Here are some <var>pattern</var> examples:
  277. </p><dl compact="compact">
  278. <dt><code>%[abcd]</code></dt>
  279. <dd><p>matches strings containing only <code>a</code>, <code>b</code>, <code>c</code>, and <code>d</code>.
  280. </p>
  281. </dd>
  282. <dt><code>%[^abcd]</code></dt>
  283. <dd><p>matches strings containing any characters except <code>a</code>, <code>b</code>,
  284. <code>c</code>, or <code>d</code>
  285. </p>
  286. </dd>
  287. <dt><code>%[A-DW-Z]</code></dt>
  288. <dd><p>matches strings containing <code>A</code>, <code>B</code>, <code>C</code>, <code>D</code>, <code>W</code>,
  289. <code>X</code>, <code>Y</code>, <code>Z</code>
  290. </p>
  291. </dd>
  292. <dt><code>%[z-a]</code></dt>
  293. <dd><p>matches the characters <code>z</code>, <code>-</code>, and <code>a</code>
  294. </p></dd>
  295. </dl>
  296. <p>Floating point numbers (for field types <code>e</code>, <code>f</code>, <code>g</code>, <code>E</code>,
  297. <code>F</code>, <code>G</code>) must correspond to the following general form:
  298. </p>
  299. <div class="smallexample">
  300. <pre class="smallexample"> [+/-] ddddd[.]ddd [E|e[+|-]ddd]
  301. </pre></div>
  302. <p>where objects inclosed in square brackets are optional, and <code>ddd</code>
  303. represents decimal, octal, or hexadecimal digits.
  304. </p></li></ul>
  305. <br>
  306. <p><strong>Returns</strong><br>
  307. <code>scanf</code> returns the number of input fields successfully
  308. scanned, converted and stored; the return value does
  309. not include scanned fields which were not stored.
  310. </p>
  311. <p>If <code>scanf</code> attempts to read at end-of-file, the return
  312. value is <code>EOF</code>.
  313. </p>
  314. <p>If no fields were stored, the return value is <code>0</code>.
  315. </p>
  316. <p><code>scanf</code> might stop scanning a particular field before
  317. reaching the normal field end character, or may
  318. terminate entirely.
  319. </p>
  320. <p><code>scanf</code> stops scanning and storing the current field
  321. and moves to the next input field (if any)
  322. in any of the following situations:
  323. </p>
  324. <ul>
  325. <li> The assignment suppressing character (<code>*</code>) appears
  326. after the <code>%</code> in the format specification; the current
  327. input field is scanned but not stored.
  328. </li><li> <var>width</var> characters have been read (<var>width</var> is a
  329. width specification, a positive decimal integer).
  330. </li><li> The next character read cannot be converted
  331. under the the current format (for example,
  332. if a <code>Z</code> is read when the format is decimal).
  333. </li><li> The next character in the input field does not appear
  334. in the search set (or does appear in the inverted search set).
  335. </li></ul>
  336. <p>When <code>scanf</code> stops scanning the current input field for one of
  337. these reasons, the next character is considered unread and
  338. used as the first character of the following input field, or the
  339. first character in a subsequent read operation on the input.
  340. </p>
  341. <p><code>scanf</code> will terminate under the following circumstances:
  342. </p>
  343. <ul>
  344. <li> The next character in the input field conflicts
  345. with a corresponding non-whitespace character in the
  346. format string.
  347. </li><li> The next character in the input field is <code>EOF</code>.
  348. </li><li> The format string has been exhausted.
  349. </li></ul>
  350. <p>When the format string contains a character sequence that is
  351. not part of a format specification, the same character
  352. sequence must appear in the input; <code>scanf</code> will
  353. scan but not store the matched characters. If a
  354. conflict occurs, the first conflicting character remains in the input
  355. as if it had never been read.
  356. </p>
  357. <br>
  358. <p><strong>Portability</strong><br>
  359. <code>scanf</code> is ANSI C.
  360. </p>
  361. <p>Supporting OS subroutines required: <code>close</code>, <code>fstat</code>, <code>isatty</code>,
  362. <code>lseek</code>, <code>read</code>, <code>sbrk</code>, <code>write</code>.
  363. </p>
  364. <br>
  365. <hr>
  366. <div class="header">
  367. <p>
  368. Next: <a href="stdio_005fext.html#stdio_005fext" accesskey="n" rel="next">stdio_ext</a>, Previous: <a href="sprintf.html#sprintf" accesskey="p" rel="prev">sprintf</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>
  369. </div>
  370. </body>
  371. </html>