bharat_upmanyu

Untitled

Apr 20th, 2021
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. struct person
  2. {
  3. string name{""};
  4. int age{0};
  5. string sex{""};
  6. };
  7.  
  8. int main()
  9. {
  10. vector<person> v;
  11. vector<person> v1;
  12. person p;
  13.  
  14. {
  15. p.name="adam";
  16. p.age=22;
  17. p.sex="male";
  18. v.push_back(p);
  19. }
  20. {
  21. p.name="shaun";
  22. p.age=23;
  23. p.sex="male";
  24. v.push_back(p);
  25. }
  26. {
  27. p.name="megan";
  28. p.age=24;
  29. p.sex="female";
  30. v.push_back(p);
  31. }
  32.  
  33.  
  34. ofstream os("record.dat",ios_base::binary|ios_base::app);
  35.  
  36. for(int i=0;i<v.size();++i)
  37. {
  38. os.write((char*) &v[i],sizeof(p));
  39. }
  40. os.close();
  41.  
  42.  
  43.  
  44. ifstream is("record.dat",ios_base::binary);
  45.  
  46. if(!is) cout<<"file not found";
  47.  
  48.  
  49. while(is.read((char*)&p,sizeof(p)))
  50. {
  51. v1.push_back(p);
  52. }
  53. is.close();
  54.  
  55. for(auto a:v1)
  56. {
  57. cout<<a.name<<" "<<a.age<<" "<<a.sex<<endl;
  58. }
  59.  
  60.  
  61. return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment