summaryrefslogtreecommitdiff
path: root/devdocs/c/io%2Ffwscanf.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/c/io%2Ffwscanf.html')
-rw-r--r--devdocs/c/io%2Ffwscanf.html216
1 files changed, 216 insertions, 0 deletions
diff --git a/devdocs/c/io%2Ffwscanf.html b/devdocs/c/io%2Ffwscanf.html
new file mode 100644
index 00000000..69582d63
--- /dev/null
+++ b/devdocs/c/io%2Ffwscanf.html
@@ -0,0 +1,216 @@
+ <h1 id="firstHeading" class="firstHeading">wscanf, fwscanf, swscanf, wscanf_s, fwscanf_s, swscanf_s</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;wchar.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl-rev-aux"> <td></td> <td rowspan="3">(1)</td> <td></td> </tr> <tr class="t-dcl t-since-c95 t-until-c99"> <td> <pre data-language="c">int wscanf( const wchar_t *format, ... );</pre>
+</td> <td> <span class="t-mark-rev t-since-c95">(since C95)</span> <br><span class="t-mark-rev t-until-c99">(until C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">int wscanf( const wchar_t *restrict format, ... );</pre>
+</td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl-rev-aux"> <td></td> <td rowspan="3">(2)</td> <td></td> </tr> <tr class="t-dcl t-since-c95 t-until-c99"> <td> <pre data-language="c">int fwscanf( FILE *stream, const wchar_t *format, ... );</pre>
+</td> <td> <span class="t-mark-rev t-since-c95">(since C95)</span> <br><span class="t-mark-rev t-until-c99">(until C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">int fwscanf( FILE *restrict stream,
+ const wchar_t *restrict format, ... );</pre>
+</td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl-rev-aux"> <td></td> <td rowspan="3">(3)</td> <td></td> </tr> <tr class="t-dcl t-since-c95 t-until-c99"> <td> <pre data-language="c">int swscanf( const wchar_t *buffer, const wchar_t *format, ... );</pre>
+</td> <td> <span class="t-mark-rev t-since-c95">(since C95)</span> <br><span class="t-mark-rev t-until-c99">(until C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">int swscanf( const wchar_t *restrict buffer,
+ const wchar_t *restrict format, ... );</pre>
+</td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">int wscanf_s( const wchar_t *restrict format, ...);</pre>
+</td> <td> (4) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">int fwscanf_s( FILE *restrict stream,
+ const wchar_t *restrict format, ...);</pre>
+</td> <td> (5) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">int swscanf_s( const wchar_t *restrict s,
+ const wchar_t *restrict format, ...);</pre>
+</td> <td> (6) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> </table> <p>Reads data from the a variety of sources, interprets it according to <code>format</code> and stores the results into given locations.</p>
+<div class="t-li1">
+<span class="t-li">1)</span> Reads the data from <code><a href="std_streams" title="c/io/std streams">stdin</a></code>.</div> <div class="t-li1">
+<span class="t-li">2)</span> Reads the data from file stream <code>stream</code>.</div> <div class="t-li1">
+<span class="t-li">3)</span> Reads the data from null-terminated wide string <code>buffer</code>. Reaching the end of the string is equivalent to reaching the end-of-file condition for <code>fwscanf</code>
+</div> <div class="t-li1">
+<span class="t-li">4-6)</span> Same as <span class="t-v">(1-3)</span>, except that <code>%c</code>, <code>%s</code>, and <code>%[</code> conversion specifiers each expect two arguments (the usual pointer and a value of type <code>rsize_t</code> indicating the size of the receiving array, which may be <code>1</code> when reading with a <code>%lc</code> into a single wide character) and except that the following errors are detected at runtime and call the currently installed <a href="../error/set_constraint_handler_s" title="c/error/set constraint handler s">constraint handler</a> function: <dl>
+<dd>
+<ul>
+<li> any of the arguments of pointer type is a null pointer </li>
+<li> <code>format</code>, <code>stream</code>, or <code>buffer</code> is a null pointer </li>
+<li> the number of characters that would be written by <code>%c</code>, <code>%s</code>, or <code>%[</code>, plus the terminating null character, would exceed the second (<code>rsize_t</code>) argument provided for each of those conversion specifiers </li>
+<li> optionally, any other detectable error, such as unknown conversion specifier </li>
+</ul> </dd>
+<dd>As all bounds-checked functions, <code>wscanf_s</code>, <code>fwscanf_s</code>, and <code>swscanf_s</code> are only guaranteed to be available if <code>__STDC_LIB_EXT1__</code> is defined by the implementation and if the user defines <code>__STDC_WANT_LIB_EXT1__</code> to the integer constant <code>1</code> before including <a href="../string/wide" title="c/string/wide"><code>&lt;wchar.h&gt;</code></a>.</dd>
+</dl>
+</div> <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> stream </td> <td> - </td> <td> input file stream to read from </td>
+</tr> <tr class="t-par"> <td> buffer </td> <td> - </td> <td> pointer to a null-terminated wide string to read from </td>
+</tr> <tr class="t-par"> <td> format </td> <td> - </td> <td> pointer to a null-terminated wide string specifying how to read the input </td>
+</tr> <tr class="t-par"> <td> ... </td> <td> - </td> <td> receiving arguments. </td>
+</tr>
+</table> <p><br> The <b>format</b> string consists of</p>
+<ul>
+<li> non-whitespace wide characters except <code>%</code>: each such character in the format string consumes exactly one identical character from the input stream, or causes the function to fail if the next character on the stream does not compare equal. </li>
+<li> whitespace characters: any single whitespace character in the format string consumes all available consecutive whitespace characters from the input (determined as if by calling <a href="../string/wide/iswspace" title="c/string/wide/iswspace"><code>iswspace</code></a> in a loop). Note that there is no difference between <code>"\n"</code>, <code>" "</code>, <code>"\t\t"</code>, or other whitespace in the format string. </li>
+<li> conversion specifications. Each conversion specification has the following format: </li>
+<ul>
+<li> introductory <code>%</code> character. </li>
+<li> <span class="t-mark">(optional)</span> assignment-suppressing character <code>*</code>. If this option is present, the function does not assign the result of the conversion to any receiving argument. </li>
+<li> <span class="t-mark">(optional)</span> integer number (greater than zero) that specifies <i>maximum field width</i>, that is, the maximum number of characters that the function is allowed to consume when doing the conversion specified by the current conversion specification. Note that <code>%s</code> and <code>%[</code> may lead to buffer overflow if the width is not provided. </li>
+<li> <span class="t-mark">(optional)</span> <i>length modifier</i> that specifies the size of the receiving argument, that is, the actual destination type. This affects the conversion accuracy and overflow rules. The default destination type is different for each conversion type (see table below). </li>
+<li> conversion format specifier. </li>
+</ul>
+</ul> <p>The following format specifiers are available:</p>
+<table class="wikitable"> <tr> <th rowspan="1">Conversion<br>specifier </th> <th rowspan="1">Explanation </th> <th colspan="9">Argument type </th>
+</tr> <tr> <th colspan="2">
+<b>Length modifier →</b> </th> <th>
+<code>hh</code> <p><span class="t-mark-rev t-since-c99">(C99)</span></p>
+</th> <th>
+<code>h</code> </th> <th>(none) </th> <th>
+<code>l</code> </th> <th>
+<code>ll</code> <p><span class="t-mark-rev t-since-c99">(C99)</span></p>
+</th> <th>
+<code>j</code> <p><span class="t-mark-rev t-since-c99">(C99)</span></p>
+</th> <th>
+<code>z</code> <p><span class="t-mark-rev t-since-c99">(C99)</span></p>
+</th> <th>
+<code>t</code> <p><span class="t-mark-rev t-since-c99">(C99)</span></p>
+</th> <th>
+<code>L</code> </th>
+</tr> <tr> <th>
+<code>%</code> </th> <td>Matches literal <code>%</code>. </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td>
+</tr> <tr> <th>
+<code>c</code> </th> <td> Matches a <b>character</b> or a sequence of <b>characters</b>. <p>If a width specifier is used, matches exactly <i>width</i> wide characters (the argument must be a pointer to an array with sufficient room). Unlike %s and %[, does not append the null character to the array.</p>
+</td> <td rowspan="3" class="table-na"> <small>N/A</small> </td> <td rowspan="3" class="table-na"> <small>N/A</small> </td> <td rowspan="3">
+<div><code>char*</code></div> </td> <td rowspan="3">
+<div><code>wchar_t*</code></div> </td> <td rowspan="3" class="table-na"> <small>N/A</small> </td> <td rowspan="3" class="table-na"> <small>N/A</small> </td> <td rowspan="3" class="table-na"> <small>N/A</small> </td> <td rowspan="3" class="table-na"> <small>N/A</small> </td> <td rowspan="3" class="table-na"> <small>N/A</small> </td>
+</tr> <tr> <th>
+<code>s</code> </th> <td> Matches a sequence of non-whitespace characters (a <b>string</b>). <p>If width specifier is used, matches up to <i>width</i> or until the first whitespace character, whichever appears first. Always stores a null character in addition to the characters matched (so the argument array must have room for at least <i>width+1</i> characters)</p>
+</td>
+</tr> <tr> <th>
+<code>[</code><span class="t-spar">set</span><code>]</code> </th> <td> Matches a non-empty sequence of character from <span class="t-spar">set</span> of characters. <p>If the first character of the set is <code>^</code>, then all characters not in the set are matched. If the set begins with <code>]</code> or <code>^]</code> then the <code>]</code> character is also included into the set. It is implementation-defined whether the character <code>-</code> in the non-initial position in the scanset may be indicating a range, as in <code>[0-9]</code>. If width specifier is used, matches only up to <i>width</i>. Always stores a null character in addition to the characters matched (so the argument array must have room for at least <i>width+1</i> characters)</p>
+</td>
+</tr> <tr> <th>
+<code>d</code> </th> <td> Matches a <b>decimal integer</b>. <p>The format of the number is the same as expected by <a href="../string/wide/wcstol" title="c/string/wide/wcstol"><code>wcstol</code></a> with the value <code>10</code> for the <code>base</code> argument</p>
+</td> <td rowspan="6">
+<div>
+<code>signed char*</code> or <code>unsigned char*</code>
+</div> </td> <td rowspan="6">
+<div>
+<code>signed short*</code> or <code>unsigned short*</code>
+</div> </td> <td rowspan="6">
+<div>
+<code>signed int*</code> or <code>unsigned int*</code>
+</div> </td> <td rowspan="6">
+<div>
+<code>signed long*</code> or <code>unsigned long*</code>
+</div> </td> <td rowspan="6">
+<div>
+<code>signed long long*</code> or <code>unsigned long long*</code>
+</div> </td> <td rowspan="6">
+<div>
+<code><a href="http://en.cppreference.com/w/c/types/integer"><span class="kw118">intmax_t</span></a><span class="sy2">*</span></code> or <code><a href="http://en.cppreference.com/w/c/types/integer"><span class="kw132">uintmax_t</span></a><span class="sy2">*</span></code>
+</div> </td> <td rowspan="6">
+<div><code><a href="http://en.cppreference.com/w/c/types/size_t"><span class="kw100">size_t</span></a><span class="sy2">*</span></code></div> </td> <td rowspan="6">
+<div><code><a href="http://en.cppreference.com/w/c/types/ptrdiff_t"><span class="kw101">ptrdiff_t</span></a><span class="sy2">*</span></code></div> </td> <td rowspan="6" class="table-na"> <small>N/A</small> </td>
+</tr> <tr> <th>
+<code>i</code> </th> <td> Matches an <b>integer</b>. <p>The format of the number is the same as expected by <a href="../string/wide/wcstol" title="c/string/wide/wcstol"><code>wcstol</code></a> with the value <code>​0​</code> for the <code>base</code> argument (base is determined by the first characters parsed)</p>
+</td>
+</tr> <tr> <th>
+<code>u</code> </th> <td> Matches an unsigned <b>decimal integer</b>. <p>The format of the number is the same as expected by <a href="../string/wide/wcstoul" title="c/string/wide/wcstoul"><code>wcstoul</code></a> with the value <code>10</code> for the <code>base</code> argument.</p>
+</td>
+</tr> <tr> <th>
+<code>o</code> </th> <td> Matches an unsigned <b>octal integer</b>. <p>The format of the number is the same as expected by <a href="../string/wide/wcstoul" title="c/string/wide/wcstoul"><code>wcstoul</code></a> with the value <code>8</code> for the <code>base</code> argument</p>
+</td>
+</tr> <tr> <th>
+<code>x</code>, <code>X</code> </th> <td> Matches an unsigned <b>hexadecimal integer</b>. <p>The format of the number is the same as expected by <a href="../string/wide/wcstoul" title="c/string/wide/wcstoul"><code>wcstoul</code></a> with the value <code>16</code> for the <code>base</code> argument</p>
+</td>
+</tr> <tr> <th>
+<code>n</code> </th> <td> Returns the <b>number of characters read so far</b>. <p>No input is consumed. Does not increment the assignment count. If the specifier has assignment-suppressing operator defined, the behavior is undefined</p>
+</td>
+</tr> <tr> <th>
+<code>a</code>, <code>A</code><span class="t-mark-rev t-since-c99">(C99)</span><br><code>e</code>, <code>E</code><br><code>f</code>, <code>F</code><span class="t-mark-rev t-since-c99">(C99)</span><br><code>g</code>, <code>G</code> </th> <td> Matches a <b>floating-point number</b>. <p>The format of the number is the same as expected by <a href="../string/wide/wcstof" title="c/string/wide/wcstof"><code>wcstof</code></a></p>
+</td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td>
+<div><code>float*</code></div> </td> <td>
+<div><code>double*</code></div> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td>
+<div><code>long double*</code></div> </td>
+</tr> <tr> <th>
+<code>p</code> </th> <td> Matches implementation defined character sequence defining a <b>pointer</b>. <p><code>printf</code> family of functions should produce the same sequence using <code>%p</code> format specifier</p>
+</td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td>
+<div><code>void**</code></div> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td>
+</tr>
+</table> <p>For every conversion specifier other than <code>n</code>, the longest sequence of input characters which does not exceed any specified field width and which either is exactly what the conversion specifier expects or is a prefix of a sequence it would expect, is what's consumed from the stream. The first character, if any, after this consumed sequence remains unread. If the consumed sequence has length zero or if the consumed sequence cannot be converted as specified above, the matching failure occurs unless end-of-file, an encoding error, or a read error prevented input from the stream, in which case it is an input failure.</p>
+<p>All conversion specifiers other than <code>[</code>, <code>c</code>, and <code>n</code> consume and discard all leading whitespace characters (determined as if by calling <a href="../string/wide/iswspace" title="c/string/wide/iswspace"><code>iswspace</code></a>) before attempting to parse the input. These consumed characters do not count towards the specified maximum field width.</p>
+<p>If the length specifier <code>l</code> is not used, the conversion specifiers <code>c</code>, <code>s</code>, and <code>[</code> perform wide-to-multibyte character conversion as if by calling <a href="../string/multibyte/wcrtomb" title="c/string/multibyte/wcrtomb"><code>wcrtomb</code></a> with an <a href="../string/multibyte/mbstate_t" title="c/string/multibyte/mbstate t"><code>mbstate_t</code></a> object initialized to zero before the first character is converted.</p>
+<p>The conversion specifiers <code>s</code> and <code>[</code> always store the null terminator in addition to the matched characters. The size of the destination array must be at least one greater than the specified field width. The use of <code>%s</code> or <code>%[</code>, without specifying the destination array size, is as unsafe as <code><a href="gets" title="c/io/gets">gets</a></code>.</p>
+<p>The correct conversion specifications for the <a href="../types/integer" title="c/types/integer">fixed-width integer types</a> (<code><a href="../types/integer" title="c/types/integer">int8_t</a></code>, etc) are defined in the header <a href="../types/integer" title="c/types/integer"><code>&lt;inttypes.h&gt;</code></a> (although <a href="../types/integer" title="c/types/integer"><code>SCNdMAX</code></a>, <a href="../types/integer" title="c/types/integer"><code>SCNuMAX</code></a>, etc is synonymous with <code>%jd</code>, <code>%ju</code>, etc).</p>
+<p>There is a <a href="../language/eval_order" title="c/language/eval order">sequence point</a> after the action of each conversion specifier; this permits storing multiple fields in the same "sink" variable.</p>
+<p>When parsing an incomplete floating-point value that ends in the exponent with no digits, such as parsing <code>"100er"</code> with the conversion specifier <code>%f</code>, the sequence <code>"100e"</code> (the longest prefix of a possibly valid floating-point number) is consumed, resulting in a matching error (the consumed sequence cannot be converted to a floating-point number), with <code>"r"</code> remaining. Some existing implementations do not follow this rule and roll back to consume only <code>"100"</code>, leaving <code>"er"</code>, e.g. <a rel="nofollow" class="external text" href="https://sourceware.org/bugzilla/show_bug.cgi?id=1765">glibc bug 1765</a>.</p>
+<h3 id="Return_value"> Return value</h3> <div class="t-li1">
+<span class="t-li">1-3)</span> Number of receiving arguments successfully assigned, or <code><a href="../io" title="c/io">EOF</a></code> if read failure occurs before the first receiving argument was assigned.</div> <div class="t-li1">
+<span class="t-li">4-6)</span> Same as <span class="t-v">(1-3)</span>, except that <code><a href="../io" title="c/io">EOF</a></code> is also returned if there is a runtime constraint violation.</div> <h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
+#include &lt;wchar.h&gt;
+#include &lt;string.h&gt;
+
+#define NUM_VARS 3
+#define ERR_READ 2
+#define ERR_WRITE 3
+
+int main(void) {
+ wchar_t state[64];
+ wchar_t capital[64];
+ unsigned int population = 0;
+ int elevation = 0;
+ int age = 0;
+ float pi = 0;
+
+#if INTERACTIVE_MODE
+ wprintf(L"Enter state, age, and pi value: ");
+ if (wscanf(L"%ls%d%f", state, &amp;age, &amp;pi) != NUM_VARS) {
+ fprintf(stderr, "Error reading input.\n");
+ return ERR_READ;
+ }
+#else
+ wchar_t* input = L"California 170 3.141592";
+ if (swscanf(input, L"%ls%d%f", state, &amp;age, &amp;pi) != NUM_VARS) {
+ fprintf(stderr, "Error reading input.\n");
+ return ERR_READ;
+ }
+#endif
+ wprintf(L"State: %ls\nAge : %d years\nPi : %.5f\n\n", state, age, pi);
+
+ FILE* fp = tmpfile();
+ if (fp) {
+ // write some data to temp file
+ if (!fwprintf(fp, L"Mississippi Jackson 420000 807")) {
+ fprintf(stderr, "Error writing to file.\n");
+ fclose(fp);
+ return ERR_WRITE;
+ }
+ // rewind file pointer
+ rewind(fp);
+
+ // read data into variables
+ fwscanf(fp, L"%ls%ls%u%d", state, capital, &amp;population, &amp;elevation);
+ wprintf(L"State : %ls\nCapital: %ls\nJackson population (in 2020): %u\n"
+ L"Highest elevation: %dft\n",
+ state, capital, population, elevation);
+ fclose(fp);
+ }
+}</pre></div> <p>Possible output:</p>
+<div class="text source-text"><pre data-language="c">State: California
+Age : 170 years
+Pi : 3.14159
+
+State : Mississippi
+Capital: Jackson
+Jackson population (in 2020): 420000
+Highest elevation: 807ft</pre></div> </div> <h3 id="References"> References</h3> <ul>
+<li> C11 standard (ISO/IEC 9899:2011): </li>
+<ul>
+<li> 7.29.2.2 The fwscanf function (p: 410-416) </li>
+<li> 7.29.2.4 The swscanf function (p: 417) </li>
+<li> 7.29.2.12 The wscanf function (p: 421) </li>
+<li> K.3.9.1.2 The fwscanf_s function (p: 628-629) </li>
+<li> K.3.9.1.5 The swscanf_s function (p: 631) </li>
+<li> K.3.9.1.14 The wscanf_s function (p: 638) </li>
+</ul>
+<li> C99 standard (ISO/IEC 9899:1999): </li>
+<ul>
+<li> 7.24.2.2 The fwscanf function (p: 356-362) </li>
+<li> 7.24.2.4 The swscanf function (p: 362) </li>
+<li> 7.24.2.12 The wscanf function (p: 366-367) </li>
+</ul>
+</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="vfwscanf" title="c/io/vfwscanf"> <span class="t-lines"><span>vwscanf</span><span>vfwscanf</span><span>vswscanf</span><span>vwscanf_s</span><span>vfwscanf_s</span><span>vswscanf_s</span></span></a></div>
+<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> reads formatted wide character input from <code><a href="std_streams" title="c/io/std streams">stdin</a></code>, a file stream<br> or a buffer using variable argument list <br> <span class="t-mark">(function)</span> </td>
+</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/io/c/fwscanf" title="cpp/io/c/fwscanf">C++ documentation</a></span> for <code>wscanf, fwscanf, swscanf</code> </td>
+</tr> </table> <div class="_attribution">
+ <p class="_attribution-p">
+ &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
+ <a href="https://en.cppreference.com/w/c/io/fwscanf" class="_attribution-link">https://en.cppreference.com/w/c/io/fwscanf</a>
+ </p>
+</div>