最常用可靠方式是用std::ifstream/std::ofstream以binary模式配合read()/write()操作原始字节;关键点包括:必须指定ios::binary标志、确保指针有效和长度准确、读取后用gcount()验证、结构体需注意填充和对齐、每次I/O后检查流状态。

用 std::ifstream 和 std::ofstream 以二进制模式打开文件,配合 read() / write() 成员函数操作原始字节,是 C++ 标准库中最常用、最可靠的方式。
写入二进制文件
关键点:必须用 std::ios::binary 标志打开输出流;write() 接收 const char* 指针和字节数,需确保指针有效、长度准确。
示例:保存一个整数数组到文件
#include <fstream>
#include <iostream>
<p>int main() {
int data[] = {100, 200, 300, 400};
size_t count = sizeof(data) / sizeof(data[0]);</p><pre class="brush:php;toolbar:false;">std::ofstream file("data.bin", std::ios::binary);
if (!file) {
std::cerr << "无法打开输出文件\n";
return 1;
}
file.write(reinterpret_cast<const char*>(data), sizeof(data));
file.close();
return 0;}
立即学习“C++免费学习笔记(深入)”;
读取二进制文件
同样需用 std::ios::binary 打开;read() 不自动补零或检查边界,务必提前分配足够内存,并用 gcount() 检查实际读取字节数(尤其在文件不完整时)。
示例:从文件读回整数数组
#include <fstream>
#include <iostream>
#include <vector>
<p>int main() {
std::ifstream file("data.bin", std::ios::binary);
if (!file) {
std::cerr << "无法打开输入文件\n";
return 1;
}</p><pre class="brush:php;toolbar:false;">// 获取文件大小
file.seekg(0, std::ios::end);
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<char> buffer(size);
file.read(buffer.data(), size);
if (file.gcount() != size) {
std::cerr << "读取不完整\n";
return 1;
}
// 解析为 int 数组(假设原数据是 int)
const int* ints = reinterpret_cast<const int*>(buffer.data());
size_t count = size / sizeof(int);
for (size_t i = 0; i < count; ++i) {
std::cout << ints[i] << " ";
}
std::cout << "\n";
return 0;}
立即学习“C++免费学习笔记(深入)”;
处理结构体(含对齐与可移植性提醒)
直接读写结构体可行,但要注意:结构体可能有填充字节(padding),不同编译器/平台对齐方式不同,跨平台传输时建议手动序列化字段。
若仅限同一程序内读写,且结构体是 POD 类型,可安全使用:
struct Point {
int x;
int y;
double z;
}; // 确保无虚函数、无非POD成员
<p>Point p{1, 2, 3.14};
std::ofstream out("point.bin", std::ios::binary);
out.write(reinterpret_cast<const char*>(&p), sizeof(p));</p>读取时同理,但注意:sizeof(Point) 包含填充,必须保持定义一致;否则会错位解析。
错误检查与健壮性建议
- 每次 I/O 后检查流状态:
if (!file) {...}或if (file.fail()) {...} - 写入后调用
file.flush()确保缓冲区落盘(尤其重要场景) - 读取前用
seekg()+tellg()获取真实大小,避免依赖预设长度 - 避免用
/ <code>>>操作符处理二进制——它们是格式化文本流,会破坏原始字节
基本上就这些。核心就是“binary 标志 + read/write + reinterpret_cast + 严格字节计数”,不复杂但容易忽略细节。










