©
本文档使用
php中文网手册 发布
在头文件<stdio.h>中定义 | ||
|---|---|---|
int fclose(FILE * stream); |
关闭给定的文件流。任何未写入的缓冲数据将刷新到操作系统。任何未读取的缓冲数据都将被丢弃。
无论操作是否成功,数据流不再与文件关联,并且如果使用自动分配,则由setbuf或分配的缓冲区setvbuf也会被解除关联并解除分配。
如果指针的值stream在fclose返回后使用,则行为未定义。
流 | - | 文件流关闭 |
|---|
0 on success, EOF otherwise.
#include <stdio.h>#include <stdlib.h>
int main(void){
FILE* fp = fopen("test.txt", "r"); if(!fp) { perror("File opening failed"); return EXIT_FAILURE; }
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 reached successfully");
fclose(fp);}C11标准(ISO / IEC 9899:2011):
7.21.5.1 fclose函数(p:304)
C99标准(ISO / IEC 9899:1999):
7.19.5.1 fclose函数(p:270)
C89 / C90标准(ISO / IEC 9899:1990):
4.9.5.1 fclose函数