summaryrefslogtreecommitdiff
path: root/devdocs/c/io%2Ffgetwc.html
blob: c17f187dc2717f28e701d7b6e60eca061cb32612 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    <h1 id="firstHeading" class="firstHeading">fgetwc, getwc</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"> <td> <pre data-language="c">wint_t fgetwc( FILE *stream );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c95">(since C95)</span> </td> </tr> <tr class="t-dcl"> <td> <pre data-language="c">wint_t getwc( FILE *stream );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c95">(since C95)</span> </td> </tr>   </table> <p>Reads the next wide character from the given input stream. <code>getwc()</code> may be implemented as a macro and may evaluate <code>stream</code> more than once.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> stream </td> <td> - </td> <td> to read the wide character from </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>The next wide character from the stream or <code>WEOF</code> on failure.</p>
<p>If the failure has been caused by end-of-file condition, additionally sets the <i>eof</i> indicator (see <code><a href="feof" title="c/io/feof">feof()</a></code>) on <code>stream</code>. If the failure has been caused by some other error, sets the <i>error</i> indicator (see <code><a href="ferror" title="c/io/ferror">ferror()</a></code>) on <code>stream</code>.</p>
<p>If an encoding error occurred, additionally sets <code><a href="../error/errno" title="c/error/errno">errno</a></code> to <code>EILSEQ</code>.</p>
<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;stdlib.h&gt;
#include &lt;wchar.h&gt;
#include &lt;errno.h&gt;
#include &lt;locale.h&gt;
 
int main(void)
{
    setlocale(LC_ALL, "en_US.utf8");
    FILE *fp = fopen("fgetwc.dat", "w");
    if(!fp) {
        perror("Can't open file for writing");
        return EXIT_FAILURE;
    }
    fputs("кошка\n", fp);
    fclose(fp);
 
    fp = fopen("fgetwc.dat", "r");
    if(!fp) {
        perror("Can't open file for reading");
        return EXIT_FAILURE;
    }
    wint_t wc;
    errno = 0;
    while ((wc = fgetwc(fp)) != WEOF)
        putwchar(wc);
 
    if (ferror(fp)) {
        if (errno == EILSEQ)
            puts("Character encoding error while reading.");
        else
            puts("I/O error when reading");
    } else if (feof(fp))
        puts("End of file reached successfully");
 
    fclose(fp);
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">кошка</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 7.29.3.1 The fgetwc function (p: 307-308) </li>
<li> 7.29.3.6 The getwc function (p: 309) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.29.3.1 The fgetwc function (p: 421-422) </li>
<li> 7.29.3.6 The getwc function (p: 424) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.24.3.1 The fgetwc function (p: 367) </li>
<li> 7.24.3.6 The getwc function (p: 369) </li>
</ul>
</ul>              <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="fgetc" title="c/io/fgetc"> <span class="t-lines"><span>fgetc</span><span>getc</span></span></a></div> </td> <td> gets a character from a file stream <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="fgetws" title="c/io/fgetws"> <span class="t-lines"><span>fgetws</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span></span></div> </td> <td> gets a wide string from a file stream <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="fputwc" title="c/io/fputwc"> <span class="t-lines"><span>fputwc</span><span>putwc</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span></span></div> </td> <td> writes a wide character to a file stream <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="ungetwc" title="c/io/ungetwc"> <span class="t-lines"><span>ungetwc</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span></span></div> </td> <td> puts a wide character back into a file stream <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/fgetwc" title="cpp/io/c/fgetwc">C++ documentation</a></span> for <code>fgetwc</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/fgetwc" class="_attribution-link">https://en.cppreference.com/w/c/io/fgetwc</a>
  </p>
</div>