|
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>swscanf (The Red Hat newlib C Library)</title>
-
- <meta name="description" content="swscanf (The Red Hat newlib C Library)">
- <meta name="keywords" content="swscanf (The Red Hat newlib C Library)">
- <meta name="resource-type" content="document">
- <meta name="distribution" content="global">
- <meta name="Generator" content="makeinfo">
- <link href="index.html#Top" rel="start" title="Top">
- <link href="Document-Index.html#Document-Index" rel="index" title="Document Index">
- <link href="Document-Index.html#SEC_Contents" rel="contents" title="Table of Contents">
- <link href="Stdio.html#Stdio" rel="up" title="Stdio">
- <link href="tmpfile.html#tmpfile" rel="next" title="tmpfile">
- <link href="swprintf.html#swprintf" rel="prev" title="swprintf">
- <style type="text/css">
- <!--
- a.summary-letter {text-decoration: none}
- blockquote.indentedblock {margin-right: 0em}
- blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
- blockquote.smallquotation {font-size: smaller}
- div.display {margin-left: 3.2em}
- div.example {margin-left: 3.2em}
- div.lisp {margin-left: 3.2em}
- div.smalldisplay {margin-left: 3.2em}
- div.smallexample {margin-left: 3.2em}
- div.smalllisp {margin-left: 3.2em}
- kbd {font-style: oblique}
- pre.display {font-family: inherit}
- pre.format {font-family: inherit}
- pre.menu-comment {font-family: serif}
- pre.menu-preformatted {font-family: serif}
- pre.smalldisplay {font-family: inherit; font-size: smaller}
- pre.smallexample {font-size: smaller}
- pre.smallformat {font-family: inherit; font-size: smaller}
- pre.smalllisp {font-size: smaller}
- span.nolinebreak {white-space: nowrap}
- span.roman {font-family: initial; font-weight: normal}
- span.sansserif {font-family: sans-serif; font-weight: normal}
- ul.no-bullet {list-style: none}
- -->
- </style>
-
-
- </head>
-
- <body lang="en">
- <a name="swscanf"></a>
- <div class="header">
- <p>
- 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> [<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>
- </div>
- <hr>
- <a name="swscanf_002c-fwscanf_002c-wscanf_002d_002d_002dscan-and-format-wide-character-input"></a>
- <h3 class="section">4.65 <code>swscanf</code>, <code>fwscanf</code>, <code>wscanf</code>—scan and format wide character input</h3>
- <a name="index-wscanf"></a>
- <a name="index-_005fwscanf_005fr"></a>
- <a name="index-fwscanf"></a>
- <a name="index-_005ffwscanf_005fr"></a>
- <a name="index-swscanf"></a>
- <a name="index-_005fswscanf_005fr"></a>
- <p><strong>Synopsis</strong>
- </p><div class="example">
- <pre class="example">#include <stdio.h>
-
- int wscanf(const wchar_t *__restrict <var>format</var>, ...);
- int fwscanf(FILE *__restrict <var>fd</var>,
- const wchar_t *__restrict <var>format</var>, ...);
- int swscanf(const wchar_t *__restrict <var>str</var>,
- const wchar_t *__restrict <var>format</var>, ...);
-
- int _wscanf_r(struct _reent *<var>ptr</var>, const wchar_t *<var>format</var>, ...);
- int _fwscanf_r(struct _reent *<var>ptr</var>, FILE *<var>fd</var>,
- const wchar_t *<var>format</var>, ...);
- int _swscanf_r(struct _reent *<var>ptr</var>, const wchar_t *<var>str</var>,
- const wchar_t *<var>format</var>, ...);
-
- </pre></div>
- <p><strong>Description</strong><br>
- <code>wscanf</code> scans a series of input fields from standard input,
- one wide character at a time. Each field is interpreted according to
- a format specifier passed to <code>wscanf</code> in the format string at
- <code>*<var>format</var></code>. <code>wscanf</code> stores the interpreted input from
- each field at the address passed to it as the corresponding argument
- following <var>format</var>. You must supply the same number of
- format specifiers and address arguments as there are input fields.
- </p>
- <p>There must be sufficient address arguments for the given format
- specifiers; if not the results are unpredictable and likely
- disasterous. Excess address arguments are merely ignored.
- </p>
- <p><code>wscanf</code> often produces unexpected results if the input diverges from
- an expected pattern. Since the combination of <code>gets</code> or <code>fgets</code>
- followed by <code>swscanf</code> is safe and easy, that is the preferred way
- to be certain that a program is synchronized with input at the end
- of a line.
- </p>
- <p><code>fwscanf</code> and <code>swscanf</code> are identical to <code>wscanf</code>, other than the
- source of input: <code>fwscanf</code> reads from a file, and <code>swscanf</code>
- from a string.
- </p>
- <p>The routines <code>_wscanf_r</code>, <code>_fwscanf_r</code>, and <code>_swscanf_r</code> are reentrant
- versions of <code>wscanf</code>, <code>fwscanf</code>, and <code>swscanf</code> that take an additional
- first argument pointing to a reentrancy structure.
- </p>
- <p>The string at <code>*<var>format</var></code> is a wide character sequence composed
- of zero or more directives. Directives are composed of
- one or more whitespace characters, non-whitespace characters,
- and format specifications.
- </p>
- <p>Whitespace characters are blank (<code> </code>), tab (<code>\t</code>), or
- newline (<code>\n</code>).
- When <code>wscanf</code> encounters a whitespace character in the format string
- it will read (but not store) all consecutive whitespace characters
- up to the next non-whitespace character in the input.
- </p>
- <p>Non-whitespace characters are all other ASCII characters except the
- percent sign (<code>%</code>). When <code>wscanf</code> encounters a non-whitespace
- character in the format string it will read, but not store
- a matching non-whitespace character.
- </p>
- <p>Format specifications tell <code>wscanf</code> to read and convert characters
- from the input field into specific types of values, and store then
- in the locations specified by the address arguments.
- </p>
- <p>Trailing whitespace is left unread unless explicitly
- matched in the format string.
- </p>
- <p>The format specifiers must begin with a percent sign (<code>%</code>)
- and have the following form:
- </p>
- <div class="smallexample">
- <pre class="smallexample"> %[*][<var>width</var>][<var>size</var>]<var>type</var>
- </pre></div>
-
- <p>Each format specification begins with the percent character (<code>%</code>).
- The other fields are:
- </p><ul>
- <li> *
-
- <p>an optional marker; if present, it suppresses interpretation and
- assignment of this input field.
- </p>
- </li><li> <var>width</var>
-
- <p>an optional maximum field width: a decimal integer,
- which controls the maximum number of characters that
- will be read before converting the current input field. If the
- input field has fewer than <var>width</var> characters, <code>wscanf</code>
- reads all the characters in the field, and then
- proceeds with the next field and its format specification.
- </p>
- <p>If a whitespace or a non-convertable wide character occurs
- before <var>width</var> character are read, the characters up
- to that character are read, converted, and stored.
- Then <code>wscanf</code> proceeds to the next format specification.
- </p>
- </li><li> <var>size</var>
-
- <p><code>h</code>, <code>j</code>, <code>l</code>, <code>L</code>, <code>t</code>, and <code>z</code> are optional size
- characters which override the default way that <code>wscanf</code>
- interprets the data type of the corresponding argument.
- </p>
- <table>
- <thead><tr><th width="18%">Modifier</th><th width="30%">Type(s)</th><th width="52%"></th></tr></thead>
- <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>
- <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>
- <tr><td width="18%">h</td><td width="30%">e, f, c, s, p</td><td width="52%">no effect</td></tr>
- <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>
- <tr><td width="18%">j</td><td width="30%">all others</td><td width="52%">no effect</td></tr>
- <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>
- <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>
- <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>
- <tr><td width="18%">l</td><td width="30%">p</td><td width="52%">no effect</td></tr>
- <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>
- <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>
- <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>
- <tr><td width="18%">L</td><td width="30%">all others</td><td width="52%">no effect</td></tr>
- <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>
- <tr><td width="18%">t</td><td width="30%">all others</td><td width="52%">no effect</td></tr>
- <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>
- <tr><td width="18%">z</td><td width="30%">all others</td><td width="52%">no effect</td></tr>
- </table>
-
- </li><li> <var>type</var>
-
- <p>A character to specify what kind of conversion
- <code>wscanf</code> performs. Here is a table of the conversion
- characters:
- </p>
- <dl compact="compact">
- <dt><code>%</code></dt>
- <dd><p>No conversion is done; the percent character (<code>%</code>) is stored.
- </p>
- </dd>
- <dt><code>c</code></dt>
- <dd><p>Scans one wide character. Corresponding <var>arg</var>: <code>(char *arg)</code>.
- Otherwise, if an <code>l</code> specifier is present, the corresponding
- <var>arg</var> is a <code>(wchar_t *arg)</code>.
- </p>
- </dd>
- <dt><code>s</code></dt>
- <dd><p>Reads a character string into the array supplied.
- Corresponding <var>arg</var>: <code>(char arg[])</code>.
- If an <code>l</code> specifier is present, the corresponding <var>arg</var> is a <code>(wchar_t *arg)</code>.
- </p>
- </dd>
- <dt><code>[<var>pattern</var>]</code></dt>
- <dd><p>Reads a non-empty character string into memory
- starting at <var>arg</var>. This area must be large
- enough to accept the sequence and a
- terminating null character which will be added
- automatically. (<var>pattern</var> is discussed in the paragraph following
- this table). Corresponding <var>arg</var>: <code>(char *arg)</code>.
- If an <code>l</code> specifier is present, the corresponding <var>arg</var> is
- a <code>(wchar_t *arg)</code>.
- </p>
- </dd>
- <dt><code>d</code></dt>
- <dd><p>Reads a decimal integer into the corresponding <var>arg</var>: <code>(int *arg)</code>.
- </p>
- </dd>
- <dt><code>o</code></dt>
- <dd><p>Reads an octal integer into the corresponding <var>arg</var>: <code>(int *arg)</code>.
- </p>
- </dd>
- <dt><code>u</code></dt>
- <dd><p>Reads an unsigned decimal integer into the corresponding
- <var>arg</var>: <code>(unsigned int *arg)</code>.
- </p>
- </dd>
- <dt><code>x,X</code></dt>
- <dd><p>Read a hexadecimal integer into the corresponding <var>arg</var>:
- <code>(int *arg)</code>.
- </p>
- </dd>
- <dt><code>e, f, g</code></dt>
- <dd><p>Read a floating-point number into the corresponding <var>arg</var>:
- <code>(float *arg)</code>.
- </p>
- </dd>
- <dt><code>E, F, G</code></dt>
- <dd><p>Read a floating-point number into the corresponding <var>arg</var>:
- <code>(double *arg)</code>.
- </p>
- </dd>
- <dt><code>i</code></dt>
- <dd><p>Reads a decimal, octal or hexadecimal integer into the
- corresponding <var>arg</var>: <code>(int *arg)</code>.
- </p>
- </dd>
- <dt><code>n</code></dt>
- <dd><p>Stores the number of characters read in the corresponding
- <var>arg</var>: <code>(int *arg)</code>.
- </p>
- </dd>
- <dt><code>p</code></dt>
- <dd><p>Stores a scanned pointer. ANSI C leaves the details
- to each implementation; this implementation treats
- <code>%p</code> exactly the same as <code>%U</code>. Corresponding
- <var>arg</var>: <code>(void **arg)</code>.
- </p></dd>
- </dl>
-
-
- <p>A <var>pattern</var> of characters surrounded by square brackets can be used
- instead of the <code>s</code> type character. <var>pattern</var> is a set of
- characters which define a search set of possible characters making up
- the <code>wscanf</code> input field. If the first character in the brackets is a
- caret (<code>^</code>), the search set is inverted to include all ASCII characters
- except those between the brackets. There is no range facility as is
- defined in the corresponding non-wide character scanf functions.
- Ranges are not part of the POSIX standard.
- </p>
- <p>Here are some <var>pattern</var> examples:
- </p><dl compact="compact">
- <dt><code>%[abcd]</code></dt>
- <dd><p>matches wide character strings containing only
- <code>a</code>, <code>b</code>, <code>c</code>, and <code>d</code>.
- </p>
- </dd>
- <dt><code>%[^abcd]</code></dt>
- <dd><p>matches wide character strings containing any characters except
- <code>a</code>, <code>b</code>, <code>c</code>, or <code>d</code>.
- </p>
- </dd>
- <dt><code>%[A-DW-Z]</code></dt>
- <dd><p>Note: No wide character ranges, so this expression matches wide
- character strings containing <code>A</code>, <code>-</code>, <code>D</code>, <code>W</code>, <code>Z</code>.
- </p></dd>
- </dl>
-
-
- <p>Floating point numbers (for field types <code>e</code>, <code>f</code>, <code>g</code>, <code>E</code>,
- <code>F</code>, <code>G</code>) must correspond to the following general form:
- </p>
- <div class="smallexample">
- <pre class="smallexample"> [+/-] ddddd[.]ddd [E|e[+|-]ddd]
- </pre></div>
-
- <p>where objects inclosed in square brackets are optional, and <code>ddd</code>
- represents decimal, octal, or hexadecimal digits.
- </p></li></ul>
-
-
- <br>
- <p><strong>Returns</strong><br>
- <code>wscanf</code> returns the number of input fields successfully
- scanned, converted and stored; the return value does
- not include scanned fields which were not stored.
- </p>
- <p>If <code>wscanf</code> attempts to read at end-of-file, the return
- value is <code>EOF</code>.
- </p>
- <p>If no fields were stored, the return value is <code>0</code>.
- </p>
- <p><code>wscanf</code> might stop scanning a particular field before
- reaching the normal field end character, or may
- terminate entirely.
- </p>
- <p><code>wscanf</code> stops scanning and storing the current field
- and moves to the next input field (if any)
- in any of the following situations:
- </p>
- <ul>
- <li> The assignment suppressing character (<code>*</code>) appears
- after the <code>%</code> in the format specification; the current
- input field is scanned but not stored.
-
- </li><li> <var>width</var> characters have been read (<var>width</var> is a
- width specification, a positive decimal integer).
-
- </li><li> The next wide character read cannot be converted
- under the the current format (for example,
- if a <code>Z</code> is read when the format is decimal).
-
- </li><li> The next wide character in the input field does not appear
- in the search set (or does appear in the inverted search set).
- </li></ul>
-
-
- <p>When <code>wscanf</code> stops scanning the current input field for one of
- these reasons, the next character is considered unread and
- used as the first character of the following input field, or the
- first character in a subsequent read operation on the input.
- </p>
- <p><code>wscanf</code> will terminate under the following circumstances:
- </p>
- <ul>
- <li> The next wide character in the input field conflicts
- with a corresponding non-whitespace character in the
- format string.
-
- </li><li> The next wide character in the input field is <code>WEOF</code>.
-
- </li><li> The format string has been exhausted.
- </li></ul>
-
-
- <p>When the format string contains a wide character sequence that is
- not part of a format specification, the same wide character
- sequence must appear in the input; <code>wscanf</code> will
- scan but not store the matched characters. If a
- conflict occurs, the first conflicting wide character remains in the
- input as if it had never been read.
- </p>
- <br>
- <p><strong>Portability</strong><br>
- <code>wscanf</code> is C99, POSIX-1.2008.
- </p>
- <p>Supporting OS subroutines required: <code>close</code>, <code>fstat</code>, <code>isatty</code>,
- <code>lseek</code>, <code>read</code>, <code>sbrk</code>, <code>write</code>.
- </p>
- <br>
-
- <hr>
- <div class="header">
- <p>
- 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> [<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>
- </div>
-
-
-
- </body>
- </html>
|