Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <string>
- using namespace std;
- struct SpecSchool
- {
- string city;
- int number;
- string spec;
- int students;
- };
- void write()
- {
- int amount;
- cout << "Amount of schools: ";
- cin >> amount;
- SpecSchool *array = new SpecSchool[amount];
- for (int i = 0; i < amount; i++) {
- cout << "Element " << i << endl;
- cout << "City: ";
- cin.ignore();
- getline(cin, array[i].city);
- cout << "Number: ";
- cin >> array[i].number;
- cout << "Spec: ";
- cin.ignore();
- getline(cin, array[i].spec);
- cout << "Students: ";
- cin >> array[i].students;
- }
- ofstream text("lab7.txt");
- text << amount << endl;
- for (int i = 0; i < amount; i++) {
- text << array[i].city << endl;
- text << array[i].number << endl;
- text << array[i].spec << endl;
- text << array[i].students << endl;
- }
- ofstream bin("lab7.bin", ios::binary);
- bin.write((char *) &amount, sizeof(int));
- for (int i = 0; i < amount; i++) {
- bin << array[i].city << endl;
- bin.write((char *) &array[i].number, sizeof(int));
- bin << array[i].spec << endl;
- bin.write((char *) &array[i].students, sizeof(int));
- }
- delete[] array;
- }
- void read()
- {
- int amount;
- cout << "Text file: " << endl;
- ifstream text("lab7.txt");
- text >> amount;
- SpecSchool *array = new SpecSchool[amount];
- for (int i = 0; i < amount; i++) {
- text.ignore();
- getline(text, array[i].city);
- text >> array[i].number;
- text.ignore();
- getline(text, array[i].spec);
- text >> array[i].students;
- }
- for (int i = 0; i < amount; i++) {
- cout << "Element " << i << endl;
- cout << "City: " << array[i].city << endl;
- cout << "Number: " << array[i].number << endl;
- cout << "Spec: " << array[i].spec << endl;
- cout << "Students: " << array[i].students << endl;
- }
- delete[] array;
- cout << endl << endl << "Bin file:" << endl;
- ifstream bin("lab7.bin", ios::binary);
- bin.read((char *) &amount, sizeof(int));
- for (int i = 0; i < amount; i++) {
- getline(bin, array[i].city);
- bin.read((char *) &array[i].number, sizeof(int));
- getline(bin, array[i].spec);
- bin.read((char *) &array[i].students, sizeof(int));
- }
- for (int i = 0; i < amount; i++) {
- cout << "Element " << i << endl;
- cout << "City: " << array[i].city << endl;
- cout << "Number: " << array[i].number << endl;
- cout << "Spec: " << array[i].spec << endl;
- cout << "Students: " << array[i].students << endl;
- }
- delete[] array;
- }
- int main()
- {
- cout << "Read(0) or write(1): ";
- int state;
- cin >> state;
- if (state == 0) {
- read();
- } else {
- write();
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment