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
|
<h1 id="firstHeading" class="firstHeading">fgets</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 t-until-c99"> <td> <pre data-language="c">char *fgets( char *str, int count, FILE *stream );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <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">char *fgets( char *restrict str, int count, FILE *restrict stream );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> </table> <p>Reads at most <code>count - 1</code> characters from the given file stream and stores them in the character array pointed to by <code>str</code>. Parsing stops if a newline character is found, in which case <code>str</code> will contain that newline character, or if end-of-file occurs. If bytes are read and no errors occur, writes a null character at the position immediately after the last character written to <code>str</code>.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> str </td> <td> - </td> <td> pointer to an element of a char array </td>
</tr> <tr class="t-par"> <td> count </td> <td> - </td> <td> maximum number of characters to write (typically the length of <code>str</code>) </td>
</tr> <tr class="t-par"> <td> stream </td> <td> - </td> <td> file stream to read the data from </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p><code>str</code> on success, null pointer on failure.</p>
<p>If the end-of-file condition is encountered, sets the <i>eof</i> indicator on <code>stream</code> (see <code><a href="feof" title="c/io/feof">feof()</a></code>). This is only a failure if it causes no bytes to be read, in which case a null pointer is returned and the contents of the array pointed to by <code>str</code> are not altered (i.e. the first byte is not overwritten with a null character).</p>
<p>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>. The contents of the array pointed to by <code>str</code> are indeterminate (it may not even be null-terminated).</p>
<h3 id="Notes"> Notes</h3> <p><a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/fgets.html">POSIX additionally requires</a> that <code>fgets</code> sets <code><a href="../error/errno" title="c/error/errno">errno</a></code> if a read error occurs.</p>
<p>Although the standard specification is <a rel="nofollow" class="external text" href="https://stackoverflow.com/questions/23388620">unclear</a> in the cases where <code>count<=1</code>, common implementations do</p>
<ul>
<li> if <code>count < 1</code>, do nothing, report error </li>
<li> if <code>count == 1</code>, </li>
<ul>
<li> some implementations do nothing, report error, </li>
<li> others read nothing, store zero in <code>str[0]</code>, report success </li>
</ul>
</ul> <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)
{
FILE* tmpf = tmpfile();
fputs("Alan Turing\n", tmpf);
fputs("John von Neumann\n", tmpf);
fputs("Alonzo Church\n", tmpf);
rewind(tmpf);
char buf[8];
while (fgets(buf, sizeof buf, tmpf) != NULL)
printf("\"%s\"\n", buf);
if (feof(tmpf))
puts("End of file reached");
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">"Alan Tu"
"ring
"
"John vo"
"n Neuma"
"nn
"
"Alonzo "
"Church
"
End of file reached</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 7.21.7.2 The fgets function (p: 241) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.21.7.2 The fgets function (p: 331) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.19.7.2 The fgets function (p: 296) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.9.7.2 The fgets function </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="fscanf" title="c/io/fscanf"> <span class="t-lines"><span>scanf</span><span>fscanf</span><span>sscanf</span><span>scanf_s</span><span>fscanf_s</span><span>sscanf_s</span></span></a></div>
<div><span class="t-lines"><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 input from <code><a href="std_streams" title="c/io/std streams">stdin</a></code>, a file stream or a buffer <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="fputs" title="c/io/fputs"> <span class="t-lines"><span>fputs</span></span></a></div> </td> <td> writes a character string to a file stream <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="https://en.cppreference.com/w/c/experimental/dynamic/getline" title="c/experimental/dynamic/getline"> <span class="t-lines"><span>getline</span><span>getwline</span><span>getdelim</span><span>getwdelim</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-dynamic-tr t-mark-ts">(dynamic memory TR)</span></span></span></div> </td> <td> read from a stream into an automatically resized buffer until delimiter/end of line <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/fgets" title="cpp/io/c/fgets">C++ documentation</a></span> for <code>fgets</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/fgets" class="_attribution-link">https://en.cppreference.com/w/c/io/fgets</a>
</p>
</div>
|