Advertisement
gasaichan

clients_parser

Oct 7th, 2018
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.64 KB | None | 0 0
  1. // В папке проекта обязательно должны быть файлы client_info.txt и output.txt. В client_info.txt клиенты записываются в формате
  2. //Eiler Russia 2001 15000
  3. //Siyanko France 2006 10000
  4. //Stolyarov Germany 2010 25000
  5. //Eiler USA 2016 70000
  6. //Karaulin Russia 2010 10000
  7. //Фамилия Страна Год Цена
  8. //
  9. //
  10. //
  11. //
  12. //При запуске сначала нужно начать 1, чтобы инициализировался вектор clients. Без этого ни одна функция не будет работать!
  13. // Каждый раз при нажатии 1 дополняется файл output.txt. Это не страшно, потому что при считывании из бинарного файла
  14. // считывается ровно столько, сколько находится клиентов в clients_info.txt
  15.  
  16. #include <iostream>
  17. #include <Windows.h>
  18. #include <string>
  19. #include <fstream>
  20. #include <vector>
  21.  
  22. using namespace std;
  23.  
  24. struct Client {
  25.     string second_name;
  26.         string country;
  27.         int year;
  28.         int price;
  29. };
  30.  
  31.  
  32. // Returns vector of Client struct if everything OK, empty vector otherwise
  33. vector<Client> get_clients_from_file(string path) {
  34.     ifstream input_file;
  35.     vector<Client> clients;
  36.     input_file.open(path, ifstream::in);
  37.     if (!input_file) {
  38.         cout << "Error opening file" << endl;
  39.         return clients;
  40.     }
  41.  
  42.     else {
  43.         while (!input_file.eof()) {
  44.             Client current_client;
  45.             input_file >> current_client.second_name;
  46.             input_file >> current_client.country;
  47.             input_file >> current_client.year;
  48.             input_file >> current_client.price;
  49.             clients.push_back(current_client);
  50.         }
  51.  
  52.         input_file.close();
  53.         return clients;
  54.     }
  55.     return clients;
  56. }
  57.  
  58. void  write_bytes(string path, vector<Client> clients) {
  59.     ofstream output_file;
  60.     size_t clients_size = clients.size();
  61.     output_file.open(path, ios::binary || fstream::out);
  62.     if (!output_file) {
  63.         cout << "Error opening output file!" << endl;
  64.     }
  65.     else {
  66.         output_file.write(reinterpret_cast<const char *>(&clients_size), sizeof(clients_size));
  67.         output_file.write(reinterpret_cast<const char *>(&clients[0]), clients.size() * sizeof Client);
  68.         output_file.close();
  69.     }
  70. }
  71.  
  72. // Returns vector of Client struct if everything OK, empty vector otherwise
  73. vector<Client> get_bytes(string path) {
  74.     size_t clients_size;
  75.     ifstream input_file;
  76.     input_file.open(path, ios::binary);
  77.     Client client;
  78.     if (!input_file) {
  79.         cout << "Error opening file!" << endl;
  80.     }
  81.     else {
  82.         input_file.read(reinterpret_cast<char *>(&clients_size), sizeof(clients_size));
  83.         vector<Client> clients(clients_size);
  84.  
  85.         input_file.read(reinterpret_cast<char *>(&clients[0]), sizeof(Client) * clients_size);
  86.         return clients;
  87.     }
  88. }
  89.  
  90. int main() {
  91.     setlocale(LC_ALL, "Russian");
  92.  
  93.     int choice = 0;
  94.     vector<Client> clients;
  95.     vector<Client> clients_from_binary;
  96.     string country;
  97.  
  98.     while (true) {
  99.         cout << "1. Считать список клиентов из файла client_info.txt и записать в бинарный файл output.txt" << endl;
  100.         cout << "2. Вывести список клиентов на экран" << endl;
  101.         cout << "3. Очистить экран" << endl;
  102.         cout << "4. Найти всех, клиентов, посетивших страну X " << endl;
  103.         cout << "5. Считать клиентов из бинарного файла и вывести на экран" << endl;
  104.         cout << "6. Выход" << endl;
  105.         cout << "Ваш выбор: ";
  106.         cin >> choice;
  107.  
  108.         switch (choice) {
  109.         case 1:
  110.             clients = get_clients_from_file("client_info.txt");
  111.             write_bytes("output.txt", clients);
  112.             system("CLS");
  113.             break;
  114.         case 2:
  115.             system("CLS");
  116.  
  117.             for (auto i : clients) {
  118.                 cout << "Фамилия: " << i.second_name << "Страна: " << i.country << "Год: " << i.year << "Цена: " << i.price << endl;
  119.             }
  120.             break;
  121.         case 3:
  122.             system("CLS");
  123.             break;
  124.         case 4:
  125.             system("CLS");
  126.             cout << "Введите страну: ";
  127.             cin >> country;
  128.             for (auto i : clients) {
  129.                 if (i.country == country) {
  130.                     cout << "Фамилия: " << i.second_name << "Страна: " << i.country << "Год: " << i.year << "Цена: " << i.price << endl;
  131.                 }
  132.             }
  133.             break;
  134.         case 5:
  135.             system("CLS");
  136.  
  137.             clients_from_binary = get_bytes("output.txt");
  138.             for (auto i : clients_from_binary) {
  139.                 cout << "Фамилия: " << i.second_name << "Страна: " << i.country << "Год: " << i.year << "Цена: " << i.price << endl;
  140.  
  141.             }
  142.             break;
  143.         case 6:
  144.             system("EXIT");
  145.             return 0;
  146.         }
  147.        
  148.  
  149.     }
  150.  
  151.     system("PAUSE");
  152.     return 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement