std::fclose
來自 cppreference.com
定義於標頭檔案 <cstdio> |
||
int fclose( std::FILE* stream ); |
||
關閉給定的檔案流,並將任何未寫入的資料從 stream 的緩衝區寫入關聯的輸出裝置。任何未讀取的緩衝資料都將被丟棄。
無論操作是否成功,該流都不再與檔案關聯,並且由 std::setbuf 或 std::setvbuf 分配的緩衝區(如果有)也將取消關聯並在使用自動分配時解除分配。
如果任何資料被寫入輸出裝置,從 |
(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; // Note: int, not char, required to handle EOF while ((c = std::fgetc(fp)) != EOF) // Standard C I/O file reading loop 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> 的公有成員函式) | |
有關 fclose 的C 文件
|