std:: fclose
| I/O manipulators | ||||
| Print functions (C++23) | ||||
| C-style I/O | ||||
| Buffers | ||||
|
(C++23)
|
||||
|
(
C++98/26*
)
|
||||
|
(C++20)
|
||||
| Streams | ||||
| Abstractions | ||||
| File I/O | ||||
| String I/O | ||||
| Array I/O | ||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(
C++98/26*
)
|
||||
|
(
C++98/26*
)
|
||||
|
(
C++98/26*
)
|
||||
| Synchronized Output | ||||
|
(C++20)
|
||||
| Types | ||||
| Error category interface | ||||
|
(C++11)
|
||||
|
(C++11)
|
| Types and objects | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<cstdio>
|
||
|
int
fclose
(
std::
FILE
*
stream
)
;
|
||
关闭给定的文件流,并将 stream 缓冲区中所有未写入的数据写入关联的输出设备。所有未读取的缓冲数据将被丢弃。
无论操作是否成功,该流不再与文件关联,且由 std::setbuf 或 std::setvbuf 分配的缓冲区(若存在)也将解除关联,若采用自动分配方式则该缓冲区会被同时释放。
|
若向输出设备写入任何数据,从
|
(since C++26) |
如果在
std::fclose
返回后使用指针
stream
的值,其行为是未定义的。
目录 |
参数
| stream | - | 要关闭的文件流 |
返回值
0 表示成功, EOF 表示失败。
示例
#include <cstdio> #include <cstdlib> int main() { int is_ok = EXIT_FAILURE; FILE* fp = std::fopen("/tmp/test.txt", "w+"); if (!fp) { std::perror("File opening failed"); return is_ok; } int c; // 注意:必须使用 int 类型而非 char 类型来处理 EOF while ((c = std::fgetc(fp)) != EOF) // 标准 C I/O 文件读取循环 std::putchar(c); if (std::ferror(fp)) std::puts("I/O error when reading"); else if (std::feof(fp)) { std::puts("End of file reached successfully"); is_ok = EXIT_SUCCESS; } std::fclose(fp); return is_ok; }
输出:
End of file reached successfully
参见
|
打开文件
(函数) |
|
|
使用不同名称重新打开现有流
(函数) |
|
|
刷新输出区域缓冲区并关闭关联文件
(
std::basic_filebuf<CharT,Traits>
的公开成员函数)
|
|
|
C 文档
关于
fclose
|
|