person.h
#include<cstring>
class person
{
private:
char name[8];
int phone_num;
public:
person(char s[]="abc",int n=0){
strcpy(name,s);
phone_num=n;
}
char *getName(){
return name;
}
int getPhone()
{
return phone_num;
}
};
BinaryFileOutput.cpp
#include<iostream>
#include<fstream>
#include"person.h"
using namespace std;
int main()
{
ofstream output("test.txt",ios::binary);
person group[3]={person("Marry",100),person("Tom",101),person("Jack",102)};
output.write((char*)group,sizeof(group));
return 0;
}
BinaryFileInput.cpp
#include<iostream>
#include<fstream>
#include"person.h"
using namespace std;
int main()
{
ifstream input("test.txt",ios::binary);
person *temp_person=new person[3];
input.read((char*)temp_person,sizeof(person)*3);
for(int i=0;i<3;i++)
{
cout<<temp_person[i].getName()<<" "<<temp_person[i].getPhone()<<endl;
}
return 0;
}