Advertisement
fabis_sparks

CPPLab2-2

May 18th, 2018
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream>
  5. #include <iomanip>
  6. using namespace std;
  7. int FileOpenError(){
  8.     cout << "Сan't create the file or file is busy. Exiting program...\n";
  9.     system("pause");
  10.     exit(1);
  11. }
  12. class AddressBook
  13. {
  14. private:
  15.     string book_name;
  16.     string lastname, firstname, phonenumber, email;
  17. public:
  18.     AddressBook(string book_name){
  19.         ofstream adbook;
  20.         adbook.open(book_name, ios::in);
  21.         if (!adbook.is_open()) {
  22.             FileOpenError();
  23.         }
  24.         else {
  25.             cout << "File Successfully Opened!";
  26.             adbook.close();
  27.         }
  28.     }
  29.     void read(string book_name) {
  30.         ifstream adbook;
  31.             adbook.open(book_name, ios_base::in);    // открыть файл на чтение
  32.             string str;            // статический буфер строки
  33.              // Считывать и отображать строки в цикле, пока не eof
  34.                 while (getline(adbook, str))
  35.                 {
  36.                     cout << str << '\n';
  37.                 }
  38.             adbook.close();
  39.     }
  40.     void append(string book_name) {
  41.         cout << "\nEnter firstname: "; cin >> firstname;
  42.         cout << "\nEnter lastname: "; cin >> lastname;
  43.         cout << "\nEnter phonenumber: "; cin >> phonenumber;
  44.         cout << "\nEnter email: "; cin >> email;
  45.         ofstream adbook;
  46.         adbook.open(book_name, ios_base::in | ios_base::out | ios_base::app);
  47.         adbook << firstname << "\t" << lastname << "\t" << phonenumber << "\t" << email << endl;
  48.         adbook.close();
  49.     }
  50.     void rewrite(string book_name) {
  51.         ofstream adbook;
  52.         adbook.open(book_name, ios_base::in | ios_base::out | ios_base::trunc);
  53.         adbook.close();
  54.         append(book_name);
  55.     }
  56. };
  57.  
  58. int main(int argc, char* argv[])
  59. {
  60.     setlocale(LC_ALL, "Russian");
  61.     string book_name;
  62.     cout << "Enter a filename for addressbook: "; cin >> book_name;
  63.     AddressBook book(book_name);
  64.     cout << "\n\n" << "Select menu item: \n";
  65.     cout << "1. Read the file\n";
  66.     cout << "2. Append line to the file\n\n";
  67.     cout << "5. Rewrite file\n";
  68.     cout << "9. Exit\n";
  69.     cout << "Item: ";
  70.     int n;
  71.     cin >> n; cout << endl;
  72.     switch (n) {
  73.     case 1: { book.read(book_name); break; }
  74.     case 2: { book.append(book_name); break; }
  75.     case 5: { book.rewrite(book_name); break; }
  76.     case 9: { cout << "Выход из программы\n"; exit(1); }
  77.     }
  78.     system("pause");
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement