c ++ file operations of binary files - write file

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class Person {
public:
    char name[64];
    int age;
};
void test() {
    ofstream ofs;
    ofs.open("person.txt", ios::out | ios::binary);
    P the Person = { " John Doe " , 18 };
    ofs.write((const char*)&p, sizeof(Person));
    ofs.close();
}
int main () {
    test();
    system("pause");
    return 0;
}

Guess you like

Origin www.cnblogs.com/xiximayou/p/12103094.html