fclose() Close a file stream

fclose() Close a file stream

fclose() flushes and closes a stream, returning 0 on success or EOF on error. Check delayed write failures; never reuse or close the pointer again.

Categorystdio.h I/O
KindLibrary function
Header<stdio.h>
C standardC89 / C90

📝 Syntax

int fclose(FILE *stream);

⚙️ Parameters

stream RequiredA valid stream previously returned by fopen(), freopen(), tmpfile(), or a similar function.

Returns: Returns zero on success, or EOF if an error occurs.

▶ Example

Edit the C code below and click Run; the server compiles and executes it:

Output:

Stream closed successfully
⚠️ Warning Do not use the original stream pointer after closing it, and do not close the same stream twice; either action causes undefined behavior.
💡 Tip Checking fclose() is important for output files because buffered write errors can first become visible during close.

📚 Related Tutorials

❓ FAQ

QWhy should I check the return value of fclose?
AClosing flushes remaining buffered data to the underlying device, which can still fail because of a full disk or device error. Ignoring it can hide data loss.
QCan I use the original FILE pointer after fclose?
ANo. The stream object's lifetime has ended, so further I/O or another close is invalid. Reopen the file if it must be accessed again.
QCan I omit fclose before a program exits normally?
AA normal exit() or return from main flushes and closes open streams, but that is no substitute for explicit closes: long-running programs can accumulate stream resources, and per-stream close errors are difficult to handle during exit. For important output, call fclose() and check its result to catch delayed write failures.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏