欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

二进制写入文件

程序员文章站 2022-06-12 16:26:27
...

#include
#include
using namespace std;

//二进制文件 写文件

class Person
{
public:
char m_Name[64];//姓名
int m_age; //年龄
};

void test01()
{
//1.包含头文件
//2.创建流对象
fstream f;

//3.打开文件
f.open("person.txt", ios::out | ios::binary);  //ios::binary 二进制的写入方式


//4.写入文件
Person p = { "张三",20 };
f.write((const char*)&p, sizeof(Person));
//二进制写入文件的方法


//5.关闭文件
f.close();

}

int main()
{
test01();
system(“pause”);
}