Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- struct Car
- {
- std::string model;
- double fuelConsumption[3];
- double maxSpeed;
- double power;
- };
- std::ostream &operator<<(std::ostream &stream, Car &car)
- {
- stream << "Model: " << car.model << std::endl;
- stream << "Fuel consumption: "
- << car.fuelConsumption[0] << " "
- << car.fuelConsumption[1] << " "
- << car.fuelConsumption[2] << std::endl;
- stream << "Max speed: " << car.maxSpeed << std::endl;
- stream << "Power: " << car.power << std::endl;
- return stream;
- }
- int write()
- {
- size_t amount;
- std::cin >> amount;
- auto array = new Car[amount];
- for (size_t i = 0; i < amount; i++) {
- Car &car = array[i];
- std::cin.ignore();
- std::getline(std::cin, car.model);
- std::cin >> car.fuelConsumption[0];
- std::cin >> car.fuelConsumption[1];
- std::cin >> car.fuelConsumption[2];
- std::cin >> car.maxSpeed;
- std::cin >> car.power;
- }
- std::ofstream outputText("file.txt");
- outputText << amount << std::endl;
- for (size_t i = 0; i < amount; i++) {
- Car &car = array[i];
- outputText << car.model << std::endl;
- outputText << car.fuelConsumption[0] << " ";
- outputText << car.fuelConsumption[1] << " ";
- outputText << car.fuelConsumption[2] << std::endl;
- outputText << car.maxSpeed << std::endl;
- outputText << car.power << std::endl;
- }
- std::ofstream outputBinary("file.bin", std::ios::binary);
- outputBinary.write(reinterpret_cast<char *>(&amount), sizeof(size_t));
- for (size_t i = 0; i < amount; i++) {
- Car &car = array[i];
- outputBinary << car.model << std::endl;
- outputBinary.write(reinterpret_cast<char *>(&car.fuelConsumption), sizeof(double) * 3);
- outputBinary.write(reinterpret_cast<char *>(&car.maxSpeed), sizeof(double));
- outputBinary.write(reinterpret_cast<char *>(&car.power), sizeof(double));
- }
- delete[] array;
- return 0;
- }
- int read()
- {
- std::ifstream inputText("file.txt");
- size_t amount;
- inputText >> amount;
- auto array = new Car[amount];
- for (size_t i = 0; i < amount; i++) {
- Car &car = array[i];
- inputText.ignore();
- std::getline(inputText, car.model);
- inputText >> car.fuelConsumption[0];
- inputText >> car.fuelConsumption[1];
- inputText >> car.fuelConsumption[2];
- inputText >> car.maxSpeed;
- inputText >> car.power;
- std::cout << car;
- }
- delete[] array;
- std::ifstream inputBinary("file.bin", std::ios::binary);
- inputBinary.read(reinterpret_cast<char *>(&amount), sizeof(size_t));
- array = new Car[amount];
- for (size_t i = 0; i < amount; i++) {
- Car &car = array[i];
- std::getline(inputBinary, car.model);
- inputBinary.read(reinterpret_cast<char *>(&car.fuelConsumption), sizeof(double) * 3);
- inputBinary.read(reinterpret_cast<char *>(&car.maxSpeed), sizeof(double));
- inputBinary.read(reinterpret_cast<char *>(&car.power), sizeof(double));
- std::cout << car;
- }
- delete[] array;
- return 0;
- }
- int main()
- {
- size_t state;
- do {
- std::cout << "Read(0) or write(1): ";
- std::cin >> state;
- if (state == 0) {
- read();
- } else if (state == 1) {
- write();
- }
- } while (state != 0 && state != 1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement