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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
#include <iostream>
#include <fstream>
#pragma warning(disable:4996)
using namespace std;
struct Student
{
char* name;
int age;
int fn;
};
void saveStudent(ofstream& file, const Student& st)
{
size_t nameLen = strlen(st.name) + 1;
file.write((const char*)&nameLen, sizeof(nameLen));
file.write(st.name, nameLen);
file.write((const char*)&st.age, sizeof(st.age));
file.write((const char*)&st.fn, sizeof(st.fn));
}
void loadStudent(ifstream& file, Student& st)
{
size_t nameLen;
file.read((char*)&nameLen, sizeof(nameLen));
st.name = new char[nameLen];
file.read(st.name, nameLen);
file.read((char*)&st.age, sizeof(st.age));
file.read((char*)&st.fn, sizeof(st.fn));
}
int main()
{
{ //writing students to binary file
Student s1;
s1.name = new char[5];
strcpy(s1.name, "ivan");
s1.fn = 9999;
s1.age = 30;
Student s2;
s2.name = new char[6];
strcpy(s2.name, "angel");
s2.fn = 5555;
s2.age = 50;
ofstream file("Students.dat", ios::binary);
if (!file.open())
{
cout << "Error while writing to binary file!" << endl;
return -1;
}
saveStudent(file, s1);
saveStudent(file, s2);
delete[] s1.name, s2.name;
}
{
ifstream file("Students.dat", ios::binary);
if (!file.open())
{
cout << "Error while reading from file!" << endl;
return -1;
}
Student s1, s2;
loadStudent(file, s1);
loadStudent(file, s2);
std::cout << s1.name << " " << s1.age << " " << s1.fn << std::endl;
std::cout << s2.name << " " << s2.age << " " << s2.fn << std::endl;
delete[] s1.name, s2.name;
}
}
|