Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 03 / simple_struct_binary.cpp
blob: 7fe0d94c3a026d6ed10edcda64ff4f085145c0a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <fstream>
#pragma warning(disable:4996)
using namespace std;


struct Test
{
	bool b;
	int x;
};

int main()
{

	{ //write
		Test t = { true, 45 };
		ofstream file("testObj.bat", ios::binary);
		
		if (!file.open())
			return -1;

		file.write((const char*)&t, sizeof(t));
	}

	{ //read
		Test t;
		ifstream file("testObj.bat", ios::binary);
		
		if (!file.open())
			return -1;

		file.read((char*)&t, sizeof(t));
		std::cout << t.b << " " << t.x << std::endl;
	}

}