Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <string>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. struct PhoneBook
  9. {
  10. string Name;
  11. string Number;
  12. string Contact;
  13.  
  14. PhoneBook (string& InputName, string& InputNumber)
  15. {
  16. Name = InputName;
  17. Number = InputNumber;
  18. Contact = (Name + " " + Number);
  19. }
  20. bool operator < (const PhoneBook& MatchName) const
  21. {
  22. return (Name < MatchName.Name);
  23. }
  24. operator const char*() const
  25. {
  26. return Contact.c_str();
  27. }
  28. };
  29.  
  30.  
  31. int main()
  32. {
  33. set<PhoneBook> Contacts;
  34.  
  35. ofstream SaveFile("PhoneBook.txt", ios::trunc);
  36. int i = 0;
  37. while(i < 5)
  38. {
  39. string Name, Number;
  40. cout << "Name: ";
  41. cin >> Name;
  42. cout << endl << "Number: " << endl;
  43. cin >> Number;
  44. Contacts.insert(PhoneBook(Name, Number));
  45. i++;
  46. };
  47.  
  48. for(set<PhoneBook>::const_iterator iContact = Contacts.begin(); iContact != Contacts.end(); ++iContact)
  49. {
  50. SaveFile << *iContact << endl;
  51. }
  52.  
  53. SaveFile.close();
  54.  
  55. for(set<PhoneBook>::const_iterator iContact = Contacts.begin(); iContact != Contacts.end(); ++iContact)
  56. {
  57. cout << *iContact << endl;
  58. }
  59.  
  60. return 0;
  61. }
  62.  
  63. bool Phonebook::write(ostream&) const;
  64. bool Phonebook::read(istream&);
  65.  
  66. for(auto i: Contacts)
  67. if (!i.write(saveFile)) throw("Error saving phonebook");
  68.  
  69. PhoneBook p;
  70. while(p.read(loadFile))
  71. Contacts.insert(p);
  72.  
  73. friend QDataStream &operator << (QDataStream &out, const PhoneBook
  74. &phonebook) {
  75. out << phonebook.Name << phonebook.number << phonebook.Number <<
  76. phonebook.Contact;
  77. return out;
  78. }
  79.  
  80. friend QDataStream &operator >> (QDataStream &in, PhoneBook &phonebook) {
  81. out >> phonebook.Name >> phonebook.number >> phonebook.Number >>
  82. phonebook.Contact;
  83. return in;
  84. }
  85.  
  86. ofstream ofile(filename, ios::binary | ios::out);
  87. ofile << phonebook;
  88. ofile.close();
  89.  
  90. ifstream ifile(filename, ios::binary | ios::in);
  91. if (ifile.is_open()) {
  92. ifile >> phonebook;
  93. ifile.close();
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement