c++文件操作-二进制文件-写文件

#include <iostream>
#include <fstream>//包含头文件
using namespace std;
//二进制文件 写文件

class Person
{
public:
    char m_name[64];//姓名
    int m_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;
}

 

posted @ 2021-09-05 23:11  梦之心  阅读(107)  评论(0编辑  收藏  举报