Advertisement
Toliak

lab61_writer_ID

Nov 13th, 2018
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. struct Car
  6. {
  7.     std::string model;
  8.     double fuelConsumption[3];
  9.     double maxSpeed;
  10.     double power;
  11. };
  12.  
  13. int main()
  14. {
  15.     size_t amount;
  16.     std::cin >> amount;
  17.     std::cout << sizeof(std::string) << std::endl;
  18.  
  19.     auto array = new Car[amount];
  20.     for (size_t i = 0; i < amount; i++) {
  21.         Car &car = array[i];
  22.         std::cin.ignore();
  23.         std::getline(std::cin, car.model);
  24.         std::cin >> car.fuelConsumption[0];
  25.         std::cin >> car.fuelConsumption[1];
  26.         std::cin >> car.fuelConsumption[2];
  27.         std::cin >> car.maxSpeed;
  28.         std::cin >> car.power;
  29.     }
  30.  
  31.     std::ofstream outputText("file.txt");
  32.     outputText << amount << std::endl;
  33.     for (size_t i = 0; i < amount; i++) {
  34.         Car &car = array[i];
  35.         outputText << car.model << std::endl;
  36.         outputText << car.fuelConsumption[0] << " ";
  37.         outputText << car.fuelConsumption[1] << " ";
  38.         outputText << car.fuelConsumption[2] << std::endl;
  39.         outputText << car.maxSpeed << std::endl;
  40.         outputText << car.power << std::endl;
  41.     }
  42.  
  43.     std::ofstream outputBinary("file.bin", std::ios::binary);
  44.     outputBinary << amount << std::endl;
  45.     for (size_t i = 0; i < amount; i++) {
  46.         Car &car = array[i];
  47.         outputBinary.write((char *)&car, sizeof(Car));
  48.     }
  49.  
  50.     delete[] array;
  51.  
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement