Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- using namespace std;
- struct People
- {
- char surname[20];
- int age;
- char city[20];
- char edu;
- };
- void printStruct(struct People *tourist, int size)
- {
- cout << setw(10) << "SURNAME"
- << setw(5) << "AGE"
- << setw(9) << "CITY"
- << setw(8) << "EDULVL"
- << "\n";
- for (int i = 0; i < size; i++)
- {
- cout << setw(10) << (tourist + i)->surname << ' '
- << setw(4) << (tourist + i)->age << ' '
- << setw(8) << (tourist + i)->city << ' '
- << setw(4) << (tourist + i)->edu << endl;
- }
- cout << endl;
- }
- int sortStruct(struct People *tourist, int size, ostream &bin)
- {
- int sortSize = 0, *nStr = new int[size];
- for (int i = 0; i < size; i++)
- {
- if ((tourist + i)->edu == 'H')
- {
- nStr[sortSize] = i;
- sortSize++;
- }
- }
- People *sortTourist = new People[sortSize];
- for (int i = 0; i < sortSize; i++)
- sortTourist[i] = tourist[nStr[i]];
- delete[] nStr;
- bin.write(reinterpret_cast<char*> (sortTourist), sizeof(People) *sortSize);
- delete[] sortTourist;
- return sortSize;
- }
- int main()
- {
- char streamName[20], temp[255]; // НОВАЯ ПЕРЕММЕННАЯ
- int size = 0; // ЕЩЕ ОДНА НОВАЯ ПЕРЕМЕННАЯ
- cout << "Input binary file name: ";
- cin.getline(streamName, 20);
- ifstream in("textData.txt");
- while (!in.eof()) // НОВЫЙ ЦИКЛ
- {
- in.getline(temp, 255);
- size++;
- }
- in.seekg(ios_base::beg); // НОВАЯ СТРОКА
- fstream bin(streamName, ios_base::binary | ios_base::in | ios_base::out | ios_base::trunc);
- People *tourist = new People[size];
- // Чтение данных из текстового файла в массив структур
- for (int i = 0; i < size; i++) // МЕНЯЕШЬ ТУТ
- in >> (tourist + i)->surname
- >> (tourist + i)->age
- >> (tourist + i)->city
- >> (tourist + i)->edu;
- int sortSize = sortStruct(tourist, size, bin); // ТУТ
- People *binTourist = new People[sortSize];
- bin.seekg(ios_base::beg);
- bin.read(reinterpret_cast<char*> (binTourist), sizeof(People) *sortSize);
- cout << "Text file data: \n\n";
- printStruct(tourist, size); // И ТУТ
- cout << "Binary file data: \n\n";
- printStruct(binTourist, sortSize);
- delete[] tourist;
- bin.close();
- in.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement