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 。流的文件位置指示器按写入的字符数前进。
如果对象不满足 可平凡复制 要求,则行为未定义。
若发生错误,流的文件位置指示符的最终值将是不确定的。
目录 |
参数
| 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 文档
关于
fwrite
|
|