std:: fwrite
| 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>
|
||
|
std::
size_t
fwrite
(
const
void
*
buffer,
std::
size_t
size,
std::
size_t
count,
std::
FILE
*
stream
)
;
|
||
指定された配列 buffer から最大 count 個のバイナリオブジェクトを出力ストリーム stream に書き込みます。各オブジェクトは unsigned char の配列として再解釈され、 std::fputc を各オブジェクトに対して size 回呼び出すことで、これらの unsigned char を順番に stream に書き込みます。ストリームのファイル位置指示子は、書き込まれた文字数分だけ進められます。
オブジェクトが TriviallyCopyable でない場合、動作は未定義です。
エラーが発生した場合、ストリームのファイル位置指示子の結果の値は不定となります。
目次 |
パラメータ
| buffer | - | 書き込む配列の最初のオブジェクトへのポインタ |
| size | - | 各オブジェクトのサイズ |
| count | - | 書き込むオブジェクトの数 |
| stream | - | 書き込み先の出力ファイルストリーム |
戻り値
正常に書き込まれたオブジェクトの数。エラーが発生した場合、 count より少なくなる可能性があります。
size
または
count
がゼロの場合、
fwrite
はゼロを返し、それ以外の動作は行いません。
例
#include <array> #include <cstdio> #include <vector> int main () { // バッファをファイルに書き込み if (std::FILE* f1 = std::fopen("file.bin", "wb")) { std::array<int, 3> v = {42, -1, 7}; // std::arrayの基盤となるストレージは配列 std::fwrite(v.data(), sizeof v[0], v.size(), f1); std::fclose(f1); } // 同じデータを読み込み、標準出力に表示 if (std::FILE* f2 = std::fopen("file.bin", "rb")) { std::vector<int> rbuf(10); // std::vectorの基盤となるストレージも配列 std::size_t sz = std::fread(rbuf.data(), sizeof rbuf[0], rbuf.size(), f2); std::fclose(f2); for (std::size_t n = 0; n < sz; ++n) std::printf("%d\n", rbuf[n]); } }
出力:
42 -1 7
関連項目
|
(C++11)
|
書式化された出力を
stdout
、ファイルストリーム、またはバッファに出力する
(関数) |
|
文字列をファイルストリームに書き込む
(関数) |
|
|
ファイルから読み込む
(関数) |
|
|
Cドキュメント
for
fwrite
|
|