Binary File Read and Write and class struct

 


#include <iostream>
#include <fstream>
#include <string>
using namespace std;


class MyStruct
{
public:
int a;//2
float b;//4
double c;//8
}obj, obj2;

struct test
{
public:
int a;
int b;
int c;
virtual void Sum() {
cout << "test=" << (1 + 1) << endl;
}
}rt;

struct test2 :test
{
private:
int a1;
public:
int b2;
int c3;
test2() {
cout << "Constructor" << endl;
}
void TestM() {
cout << "Test" << endl;
}
void Sum() override {
cout << "test2=" << (11 + 11) << endl;
}

void SetAvalue(int a) {

}
~test2() {
cout << "Destructor" << endl;
}

};

struct test3 :test
{
public:
int x;
int y;
}obj3;

int main() {
test2 test2_obj;
test2_obj.Sum();
obj.a = 900;
obj.b = 100;
obj.c = 22000;

ofstream file("abc.txt", ios::binary | ios::out);

if (file.is_open()) {
file.write(reinterpret_cast<char*>(&obj), sizeof(obj));
file.close();
cout << "File Create Sucess.";
}

//=============Read===========

ifstream file_read("abc.txt", ios::binary | ios::out);

if (file_read.is_open()) {
file_read.read(reinterpret_cast<char*>(&obj2), sizeof(obj2));
file_read.close();
cout << "File Read Sucess.";
}


cout << "A=" << obj2.a << endl;
cout << "C=" << obj2.b << endl;
cout << "B=" << obj2.c << endl;

return 0;
}

Comments

Popular posts from this blog

Dynamic Memory2

Exception Handling 2

Smart Pointer unique_ptr, shared_ptr, weak_ptr