Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <set>
- #include <string>
- #include <fstream>
- using namespace std;
- struct PhoneBook
- {
- string Name;
- string Number;
- string Contact;
- PhoneBook (string& InputName, string& InputNumber)
- {
- Name = InputName;
- Number = InputNumber;
- Contact = (Name + " " + Number);
- }
- bool operator < (const PhoneBook& MatchName) const
- {
- return (Name < MatchName.Name);
- }
- operator const char*() const
- {
- return Contact.c_str();
- }
- };
- int main()
- {
- set<PhoneBook> Contacts;
- ofstream SaveFile("PhoneBook.txt", ios::trunc);
- int i = 0;
- while(i < 5)
- {
- string Name, Number;
- cout << "Name: ";
- cin >> Name;
- cout << endl << "Number: " << endl;
- cin >> Number;
- Contacts.insert(PhoneBook(Name, Number));
- i++;
- };
- for(set<PhoneBook>::const_iterator iContact = Contacts.begin(); iContact != Contacts.end(); ++iContact)
- {
- SaveFile << *iContact << endl;
- }
- SaveFile.close();
- for(set<PhoneBook>::const_iterator iContact = Contacts.begin(); iContact != Contacts.end(); ++iContact)
- {
- cout << *iContact << endl;
- }
- return 0;
- }
- bool Phonebook::write(ostream&) const;
- bool Phonebook::read(istream&);
- for(auto i: Contacts)
- if (!i.write(saveFile)) throw("Error saving phonebook");
- PhoneBook p;
- while(p.read(loadFile))
- Contacts.insert(p);
- friend QDataStream &operator << (QDataStream &out, const PhoneBook
- &phonebook) {
- out << phonebook.Name << phonebook.number << phonebook.Number <<
- phonebook.Contact;
- return out;
- }
- friend QDataStream &operator >> (QDataStream &in, PhoneBook &phonebook) {
- out >> phonebook.Name >> phonebook.number >> phonebook.Number >>
- phonebook.Contact;
- return in;
- }
- ofstream ofile(filename, ios::binary | ios::out);
- ofile << phonebook;
- ofile.close();
- ifstream ifile(filename, ios::binary | ios::in);
- if (ifile.is_open()) {
- ifile >> phonebook;
- ifile.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement