?
This document uses PHP Chinese website manual Release
在頭文件<stdio.h>中定義 | ||
---|---|---|
int fclose(FILE * stream); |
關閉給定的文件流。任何未寫入的緩沖數(shù)據(jù)將刷新到操作系統(tǒng)。任何未讀取的緩沖數(shù)據(jù)都將被丟棄。
無論操作是否成功,數(shù)據(jù)流不再與文件關聯(lián),并且如果使用自動分配,則由setbuf
或分配的緩沖區(qū)setvbuf
也會被解除關聯(lián)并解除分配。
如果指針的值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函數(shù)(p:304)
C99標準(ISO / IEC 9899:1999):
7.19.5.1 fclose函數(shù)(p:270)
C89 / C90標準(ISO / IEC 9899:1990):
4.9.5.1 fclose函數(shù)