Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct person
- {
- string name{""};
- int age{0};
- string sex{""};
- };
- int main()
- {
- vector<person> v;
- vector<person> v1;
- person p;
- {
- p.name="adam";
- p.age=22;
- p.sex="male";
- v.push_back(p);
- }
- {
- p.name="shaun";
- p.age=23;
- p.sex="male";
- v.push_back(p);
- }
- {
- p.name="megan";
- p.age=24;
- p.sex="female";
- v.push_back(p);
- }
- ofstream os("record.dat",ios_base::binary|ios_base::app);
- for(int i=0;i<v.size();++i)
- {
- os.write((char*) &v[i],sizeof(p));
- }
- os.close();
- ifstream is("record.dat",ios_base::binary);
- if(!is) cout<<"file not found";
- while(is.read((char*)&p,sizeof(p)))
- {
- v1.push_back(p);
- }
- is.close();
- for(auto a:v1)
- {
- cout<<a.name<<" "<<a.age<<" "<<a.sex<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment