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
|
<h1 id="firstHeading" class="firstHeading">fgetc, getc</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><stdio.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td> <pre data-language="c">int fgetc( FILE *stream );</pre>
</td> <td> (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl"> <td> <pre data-language="c">int getc( FILE *stream );</pre>
</td> <td> (2) </td> <td class="t-dcl-nopad"> </td> </tr> </table> <div class="t-li1">
<span class="t-li">1)</span> Reads the next character from the given input stream.</div> <div class="t-li1">
<span class="t-li">2)</span> Same as <code>fgetc</code>, except that if <code>getc</code> is implemented as a macro, it may evaluate stream more than once, so the corresponding argument should never be an expression with side effects.</div> <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> stream </td> <td> - </td> <td> to read the character from </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>On success, returns the obtained character as an <code>unsigned char</code> converted to an <code>int</code>. On failure, returns <code><a href="../io" title="c/io">EOF</a></code>.</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>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h>
#include <stdlib.h>
int main(void)
{
const char* fname = "/tmp/unique_name.txt"; // or tmpnam(NULL);
int is_ok = EXIT_FAILURE;
FILE* fp = fopen(fname, "w+");
if (!fp) {
perror("File opening failed");
return is_ok;
}
fputs("Hello, world!\n", fp);
rewind(fp);
int c; // note: int, not char, required to handle EOF
while ((c = fgetc(fp)) != EOF) // standard C I/O file reading loop
putchar(c);
if (ferror(fp))
puts("I/O error when reading");
else if (feof(fp)) {
puts("End of file is reached successfully");
is_ok = EXIT_SUCCESS;
}
fclose(fp);
remove(fname);
return is_ok;
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">Hello, world!
End of file is reached successfully</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 7.21.7.1 The fgetc function (p: 240-241) </li>
<li> 7.21.7.5 The getc function (p: 242) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.21.7.1 The fgetc function (p: 330) </li>
<li> 7.21.7.5 The getc function (p: 332) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.19.7.1 The fgetc function (p: 296) </li>
<li> 7.19.7.5 The getc function (p: 297-298) </li>
</ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul>
<li> 4.9.7.1 The fgetc function </li>
<li> 4.9.7.5 The getc function </li>
</ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="getchar" title="c/io/getchar"> <span class="t-lines"><span>getchar</span></span></a></div> </td> <td> reads a character from <code><a href="std_streams" title="c/io/std streams">stdin</a></code> <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="gets" title="c/io/gets"> <span class="t-lines"><span>gets</span><span>gets_s</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-until-c11">(removed in C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> reads a character string from <code>stdin</code> <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="fputc" title="c/io/fputc"> <span class="t-lines"><span>fputc</span><span>putc</span></span></a></div> </td> <td> writes a character to a file stream <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="ungetc" title="c/io/ungetc"> <span class="t-lines"><span>ungetc</span></span></a></div> </td> <td> puts a 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/fgetc" title="cpp/io/c/fgetc">C++ documentation</a></span> for <code>fgetc, getc</code> </td>
</tr> </table> <div class="_attribution">
<p class="_attribution-p">
© cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
<a href="https://en.cppreference.com/w/c/io/fgetc" class="_attribution-link">https://en.cppreference.com/w/c/io/fgetc</a>
</p>
</div>
|