Advertisement
wohyperion

Untitled

Sep 16th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. struct People
  8. {
  9.     char surname[20];
  10.     int age;
  11.     char city[20];
  12.     char edu;
  13. };
  14.  
  15. void printStruct(struct People *tourist, int size)
  16. {
  17.     cout << setw(10) << "SURNAME"
  18.         << setw(5) << "AGE"
  19.         << setw(9) << "CITY"
  20.         << setw(8) << "EDULVL"
  21.         << "\n";
  22.  
  23.     for (int i = 0; i < size; i++)
  24.     {
  25.         cout << setw(10) << (tourist + i)->surname << ' '
  26.              << setw(4) << (tourist + i)->age << ' '
  27.              << setw(8) << (tourist + i)->city << ' '
  28.              << setw(4) << (tourist + i)->edu << endl;
  29.     }
  30.     cout << endl;
  31. }
  32.  
  33. int sortStruct(struct People *tourist, int size, ostream &bin)
  34. {
  35.     int sortSize = 0, *nStr = new int[size];
  36.     for (int i = 0; i < size; i++)
  37.     {
  38.         if ((tourist + i)->edu == 'H')
  39.         {
  40.             nStr[sortSize] = i;
  41.             sortSize++;
  42.         }
  43.     }
  44.  
  45.     People *sortTourist = new People[sortSize];
  46.  
  47.     for (int i = 0; i < sortSize; i++)
  48.         sortTourist[i] = tourist[nStr[i]];
  49.  
  50.     delete[] nStr;
  51.  
  52.     bin.write(reinterpret_cast<char*> (sortTourist), sizeof(People) *sortSize);
  53.  
  54.     delete[] sortTourist;
  55.  
  56.     return sortSize;
  57. }
  58.  
  59. int main()
  60. {
  61.     char streamName[20], temp[255]; // НОВАЯ ПЕРЕММЕННАЯ
  62.     int size = 0; // ЕЩЕ ОДНА НОВАЯ ПЕРЕМЕННАЯ
  63.  
  64.     cout << "Input binary file name: ";
  65.     cin.getline(streamName, 20);
  66.  
  67.     ifstream in("textData.txt");
  68.  
  69.     while (!in.eof()) // НОВЫЙ ЦИКЛ
  70.     {
  71.         in.getline(temp, 255);
  72.         size++;
  73.     }
  74.  
  75.     in.seekg(ios_base::beg); // НОВАЯ СТРОКА
  76.  
  77.     fstream bin(streamName, ios_base::binary | ios_base::in | ios_base::out | ios_base::trunc);
  78.  
  79.     People *tourist = new People[size];
  80.  
  81.     // Чтение данных из текстового файла в массив структур
  82.     for (int i = 0; i < size; i++) // МЕНЯЕШЬ ТУТ
  83.         in  >> (tourist + i)->surname
  84.             >> (tourist + i)->age
  85.             >> (tourist + i)->city
  86.             >> (tourist + i)->edu;
  87.  
  88.     int sortSize = sortStruct(tourist, size, bin); // ТУТ
  89.     People *binTourist = new People[sortSize];
  90.  
  91.     bin.seekg(ios_base::beg);
  92.     bin.read(reinterpret_cast<char*> (binTourist), sizeof(People) *sortSize);
  93.  
  94.     cout << "Text file data: \n\n";
  95.     printStruct(tourist, size); // И ТУТ
  96.  
  97.     cout << "Binary file data: \n\n";
  98.     printStruct(binTourist, sortSize);
  99.  
  100.     delete[] tourist;
  101.  
  102.     bin.close();
  103.     in.close();
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement