Advertisement
Guest User

expl.cpp

a guest
Jun 25th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. struct A1
  7. {
  8.     char* name;
  9.     bool xex;
  10.     int age;
  11.     A1()
  12.     {
  13.         name = nullptr;
  14.         xex = false;
  15.         age = 0;
  16.     }/*
  17.     ~A1()
  18.     {
  19.         if (name != nullptr)
  20.         {
  21.             delete[] name;
  22.         }
  23.     }*/
  24.     /*friend istream& operator >>(istream& in, A1& obj)
  25.     {
  26.         char* nameCopy = new char[20];
  27.         in.getline(nameCopy, 20, ' ');
  28.         obj.name = new char[strlen(nameCopy) + 1];
  29.         strcpy_s(obj.name, strlen(nameCopy) + 1, nameCopy);
  30.         delete[] nameCopy;
  31.         char* surnameCopy = new char[20];
  32.         in.getline(surnameCopy, 20, ' ');
  33.         if (strcmp(surnameCopy, "true") == 0)
  34.         {
  35.             obj.xex = true;
  36.         }
  37.         else
  38.         {
  39.             obj.xex = false;
  40.         }
  41.         delete[] surnameCopy;
  42.         in >> obj.age;
  43.         in.get();
  44.         return in;
  45.     }*/
  46.     //friend ostream& operator << (ostream& out, A1& obj)
  47.     //{
  48.     //  out << obj.name << " " /*<< obj.xex << */" " << obj.age << endl;
  49.     //  return out;
  50.     //}
  51. };
  52.  
  53. class AA1
  54. {
  55.     int count;
  56.     A1* arr;
  57.  
  58. public:
  59.     AA1(int count_)
  60.     {
  61.         arr = new A1[count_];
  62.         count = count_;
  63.     }
  64.     AA1()
  65.     {
  66.         arr = new A1;
  67.         count = 1;
  68.     }
  69.     ifstream& ReadData(ifstream& in)
  70.     {
  71.         if (!in.is_open())
  72.             throw "File is not opened!";
  73.         if (!in)
  74.             throw "File is empty!";
  75.         int length, num;
  76.         in.seekg(0, ios::end);
  77.         length = (int)in.tellg();
  78.         num = length / sizeof(A1);
  79.         count = num;
  80.         in.clear();
  81.         in.seekg(0, ios::beg);
  82.         delete[] arr;
  83.         arr = new A1[num];
  84.         in.read((char*)arr, length);
  85.         return in;
  86.     }
  87.     ofstream& WriteData(ofstream& out)
  88.     {
  89.         if (!out) throw "Error!";
  90.         out.write((char*)arr, sizeof(A1) * count);
  91.         return out;
  92.     }
  93. };
  94.  
  95.  
  96.  
  97. int main()
  98. {
  99.     try
  100.     {
  101.         ifstream in("text.bin", ios::in | ios::binary);
  102.         ofstream out("text1.txt", ios::out | ios::binary);
  103.         AA1 obj;
  104.         obj.ReadData(in);
  105.         obj.WriteData(out);
  106.     }
  107.     catch (const char* str)
  108.     {
  109.         cout << str << endl;
  110.     }
  111.     system("pause");
  112.     return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement