1.二进制读文件主要是利用流对象调用成员函数read
2.函数原型:ifstream& read(char *buffer,int len);
参数解释:字符指针buffer指向内存中一段存储空间,len是读写的字节数
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
//二进制读文件
class Person
{
public:
char m_name[64];
int m_age;
};
void test1()
{
//读文件
ifstream ifs;
ifs.open("person.txt", ios::in | ios::binary);
if (!ifs.is_open())
{
cout << "文件打开失败" << endl;
return;
}
Person p;
ifs.read((char *)&p, sizeof(p));
cout << "姓名:" << p.m_name << " 年龄:" << p.m_age << endl;
ifs.close();
}
int main()
{
test1();
system("pause");
return 0;
}