?
本文檔使用 PHP中文網(wǎng)手冊(cè) 發(fā)布
在頭文件<stdio.h>中定義 | ||
---|---|---|
int fclose(FILE * stream); |
關(guān)閉給定的文件流。任何未寫入的緩沖數(shù)據(jù)將刷新到操作系統(tǒng)。任何未讀取的緩沖數(shù)據(jù)都將被丟棄。
無(wú)論操作是否成功,數(shù)據(jù)流不再與文件關(guān)聯(lián),并且如果使用自動(dòng)分配,則由setbuf
或分配的緩沖區(qū)setvbuf
也會(huì)被解除關(guān)聯(lián)并解除分配。
如果指針的值stream
在fclose
返回后使用,則行為未定義。
流 | - | 文件流關(guān)閉 |
---|
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標(biāo)準(zhǔn)(ISO / IEC 9899:2011):
7.21.5.1 fclose函數(shù)(p:304)
C99標(biāo)準(zhǔn)(ISO / IEC 9899:1999):
7.19.5.1 fclose函數(shù)(p:270)
C89 / C90標(biāo)準(zhǔn)(ISO / IEC 9899:1990):
4.9.5.1 fclose函數(shù)