c++文件操作之二进制文件-读文件

时间:2022-07-23
本文章向大家介绍c++文件操作之二进制文件-读文件,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class Person {
public:
    char name[64];
    int age;
};
void test() {
    ifstream ifs;
    ifs.open("person.txt", ios::in | ios::binary);
    if (!ifs.is_open()) {
        return;
    }
    Person p;
    ifs.read((char*)&p, sizeof(p));
    cout <<"p.name=" <<p.name <<","<<"p.age="<< p.age << endl;
    ifs.close();
}
int main() {
    test();
    system("pause");
    return 0;
}