Advertisement
Toliak

fixed 1

Jan 1st, 2019
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. struct Unit {
  9.     string Name;
  10.     string FIO;
  11.     int amountOfEmployes;
  12.     int amountOfFreeEmployes;
  13. };
  14.  
  15. int read() {
  16.     ifstream fin, fin_bin;
  17.     string path1 = "test.dat",
  18.         path = "test.txt";
  19.     fin.open(path);
  20.     if (!fin) {
  21.         cout << "Error open file!";
  22.     }
  23.     else {
  24.         cout << "file open!\n";
  25.         char ch;
  26.         while (fin.get(ch)) {
  27.             cout << ch;
  28.         }
  29.     }
  30.     fin.close();
  31.  
  32.     fin_bin.open(path1, ios::binary);
  33.     if (!fin_bin) {
  34.         cout << "Error open binary file!\n";
  35.     }
  36.     else {
  37.         cout << "Sucessful open binary file!\n";
  38.         fin_bin.seekg(0);
  39.         char ch = 0;
  40.         int n = 0;
  41.         fin_bin.read((char*)&n, sizeof(int));
  42.  
  43.         for (int i = 0; i < n; i++) {
  44.             string str;
  45.             int number;
  46.             getline(fin_bin, str);
  47.             std::cout << "Name of Unit: " << str << endl;
  48.             getline(fin_bin, str);
  49.             std::cout << "FIO of Chief: " << str << endl;
  50.             fin_bin.read((char*)&number, sizeof(int));
  51.             std::cout << "Number of emoployers: " << number << endl;
  52.             fin_bin.read((char*)&number, sizeof(int));
  53.             std::cout << "Number of free employers: " << number << endl;
  54.             cout << "-------------------------------------\n";
  55.         }
  56.         fin_bin.close();
  57.     }
  58.     system("pause");
  59.     return 0;
  60. }
  61.  
  62. int write() {
  63.     int n;
  64.     ofstream fin, fin_bin;
  65.     string path1 = "test.dat",
  66.         path = "test.txt";
  67.     vector<Unit> array;
  68.  
  69.     cout << "Enter number of Units: "; cin >> n;
  70.     cin.ignore();
  71.     array.resize(n);
  72.     for (int i = 0; i < n; i++) {
  73.         cout << "Enter information about about " << i+1 << " Unit:\n";
  74.         cout << "Name: "; getline(cin, array[i].Name);
  75.         cout << "FIO: "; getline(cin, array[i].FIO);
  76.         cout << "Amount of employes: "; cin >> array[i].amountOfEmployes;
  77.         cout << "Amount of free employes: "; cin >> array[i].amountOfFreeEmployes;
  78.         cin.ignore();
  79.         cout << endl;
  80.     }
  81.  
  82.     fin.open(path);
  83.     if (!fin) {
  84.         cout << "Error open file!";
  85.     }
  86.     else {
  87.         cout << "file open!\n";
  88.         for (int i = 0; i < n; i++) {
  89.             fin << array[i].Name << endl;
  90.             fin << array[i].FIO << endl;
  91.             fin << array[i].amountOfEmployes << endl;
  92.             fin << array[i].amountOfFreeEmployes << endl;
  93.         }
  94.  
  95.     }
  96.     fin.close();
  97.  
  98.     fin_bin.open(path1, ios::binary);
  99.     if (!fin_bin) {
  100.         cout << "Error open binary file!\n";
  101.     }
  102.     else {
  103.         cout << "Sucessful open binary file!\n";
  104.         fin_bin.write((char *)&n, sizeof(int));
  105.  
  106.         for (int i = 0; i < n; i++) {
  107.             fin_bin << array[i].Name << endl;
  108.             fin_bin << array[i].FIO << endl;
  109.             fin_bin.write((char*)&array[i].amountOfEmployes, sizeof(int));
  110.             fin_bin.write((char*)&array[i].amountOfFreeEmployes, sizeof(int));
  111.         }
  112.         fin_bin.close();
  113.     }
  114.     system("pause");
  115.     return 0;
  116. }
  117.  
  118. int main() {
  119.     char mode;
  120.     cout << "r/w: " << endl;
  121.     cin >> mode;
  122.     if (mode == 'r') {
  123.         return read();
  124.     }
  125.     return write();
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement