Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct A1
- {
- char* name;
- bool xex;
- int age;
- A1()
- {
- name = nullptr;
- xex = false;
- age = 0;
- }/*
- ~A1()
- {
- if (name != nullptr)
- {
- delete[] name;
- }
- }*/
- /*friend istream& operator >>(istream& in, A1& obj)
- {
- char* nameCopy = new char[20];
- in.getline(nameCopy, 20, ' ');
- obj.name = new char[strlen(nameCopy) + 1];
- strcpy_s(obj.name, strlen(nameCopy) + 1, nameCopy);
- delete[] nameCopy;
- char* surnameCopy = new char[20];
- in.getline(surnameCopy, 20, ' ');
- if (strcmp(surnameCopy, "true") == 0)
- {
- obj.xex = true;
- }
- else
- {
- obj.xex = false;
- }
- delete[] surnameCopy;
- in >> obj.age;
- in.get();
- return in;
- }*/
- //friend ostream& operator << (ostream& out, A1& obj)
- //{
- // out << obj.name << " " /*<< obj.xex << */" " << obj.age << endl;
- // return out;
- //}
- };
- class AA1
- {
- int count;
- A1* arr;
- public:
- AA1(int count_)
- {
- arr = new A1[count_];
- count = count_;
- }
- AA1()
- {
- arr = new A1;
- count = 1;
- }
- ifstream& ReadData(ifstream& in)
- {
- if (!in.is_open())
- throw "File is not opened!";
- if (!in)
- throw "File is empty!";
- int length, num;
- in.seekg(0, ios::end);
- length = (int)in.tellg();
- num = length / sizeof(A1);
- count = num;
- in.clear();
- in.seekg(0, ios::beg);
- delete[] arr;
- arr = new A1[num];
- in.read((char*)arr, length);
- return in;
- }
- ofstream& WriteData(ofstream& out)
- {
- if (!out) throw "Error!";
- out.write((char*)arr, sizeof(A1) * count);
- return out;
- }
- };
- int main()
- {
- try
- {
- ifstream in("text.bin", ios::in | ios::binary);
- ofstream out("text1.txt", ios::out | ios::binary);
- AA1 obj;
- obj.ReadData(in);
- obj.WriteData(out);
- }
- catch (const char* str)
- {
- cout << str << endl;
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement