std::fclose
From cppreference.com
| ヘッダ <cstdio>で定義 |
||
| int fclose( std::FILE* stream ); |
||
与えられたファイルストリームを閉じ、streamのバッファから関連する出力デバイスへ、書き込まれていないデータをすべて書き出す。読み込まれていないバッファデータは破棄される。
操作が成功したかどうかにかかわらず、ストリームはファイルに関連付けられなくなり、std::setbufまたはstd::setvbufによって割り当てられたバッファがあれば、それも関連付けが解除され、自動割り当てが使用されていた場合は解放される。
|
データが出力デバイスに書き込まれる場合、`std::fclose`からの戻り値は観測可能なチェックポイントを確立する。 |
(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言語ドキュメント
| |