std:: fclose
| I/O manipulators | ||||
| Print functions (C++23) | ||||
| C-style I/O | ||||
| Buffers | ||||
|
(C++23)
|
||||
|
(
C++98/26*
)
|
||||
|
(C++20)
|
||||
| Streams | ||||
| Abstractions | ||||
| File I/O | ||||
| String I/O | ||||
| Array I/O | ||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(
C++98/26*
)
|
||||
|
(
C++98/26*
)
|
||||
|
(
C++98/26*
)
|
||||
| Synchronized Output | ||||
|
(C++20)
|
||||
| Types | ||||
| Error category interface | ||||
|
(C++11)
|
||||
|
(C++11)
|
| Types and objects | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
ヘッダーで定義
<cstdio>
|
||
|
int
fclose
(
std::
FILE
*
stream
)
;
|
||
指定されたファイルストリームを閉じ、 stream のバッファから書き込まれていないデータを関連付けられた出力デバイスに書き込みます。読み込まれていないバッファデータは破棄されます。
操作が成功するかどうかに関わらず、ストリームはファイルとの関連付けが解除され、 std::setbuf または std::setvbuf によって割り当てられたバッファ(存在する場合)も関連付けが解除され、自動割り当てが使用されていた場合は解放されます。
|
出力デバイスにデータが書き込まれた場合、
|
(C++26以降) |
ポインタ
stream
の値は、
std::fclose
が返された後に使用された場合、動作は未定義です。
目次 |
パラメータ
| 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>
の公開メンバ関数)
|
|
|
Cドキュメント
for
fclose
|
|