blob: 2c80600a34e10ad838113512116788284b01b3c0 (
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
|
<h1 id="firstHeading" class="firstHeading">fclose</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 class="t-dcl-nopad"> <pre data-language="c">int fclose( FILE *stream );</pre>
</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> </table> <p>Closes the given file stream. Any unwritten buffered data are flushed to the OS. Any unread buffered data are discarded.</p>
<p>Whether or not the operation succeeds, the stream is no longer associated with a file, and the buffer allocated by <code><a href="setbuf" title="c/io/setbuf">setbuf</a></code> or <code><a href="setvbuf" title="c/io/setvbuf">setvbuf</a></code>, if any, is also disassociated and deallocated if automatic allocation was used.</p>
<p>The behavior is undefined if the value of the pointer <code>stream</code> is used after <code>fclose</code> returns.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> stream </td> <td> - </td> <td> the file stream to close </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p><code>0</code> on success, <code><a href="../io" title="c/io">EOF</a></code> otherwise</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> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.21.5.1 The fclose function (p: 304) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.19.5.1 The fclose function (p: 270) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.9.5.1 The fclose function </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="fopen" title="c/io/fopen"> <span class="t-lines"><span>fopen</span><span>fopen_s</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> opens a file <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="freopen" title="c/io/freopen"> <span class="t-lines"><span>freopen</span><span>freopen_s</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> open an existing stream with a different name <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/fclose" title="cpp/io/c/fclose">C++ documentation</a></span> for <code>fclose</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/fclose" class="_attribution-link">https://en.cppreference.com/w/c/io/fclose</a>
</p>
</div>
|