/*
* B站学习--[145 - ]
* day31 学习成果
*/
/*
*
* 5.2 二进制文件
*
* 文件打开方式要指定为 ios::binary
*
* 5.2.1 写文件
*
* 二进制方式写文件主要利用流对象调用成员函数write
*
* 函数原型;ostream& write(const char * buffer,int len);
*
* 参数:字符指针buffer指向一段存储空间。len是读写的字节数。
*
*
*
*
*/
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
//二进制文件 写文件
class Person {
public:
char Name[64]; //姓名
int Age; //年龄
};
void test01()
{
//1.包含头文件
//2.创建流对象
ofstream ofs("person.txt", ios::out | ios::binary);
//3.打开文件
//ofs.open("person.txt", ios::out | ios::binary);
//4.写文件
Person p = {"张三",18};
ofs.write((const char *)&p,sizeof(Person));
//5.关闭文件
ofs.close();
}
int main()
{
test01();
system("pause");
return 0;
}
C++基础 二进制方式写文件
最新推荐文章于 2024-07-09 22:37:42 发布