Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 03 / students_dynamicName_to_binary.cpp
blob: 33953d36ec4c26bea94cf0eaa98915d14bdcc8de (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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <fstream>

using namespace std;


struct Student
{
   char* name;
   int fn;
   int gradesCount;
   double averageGrade;
};
void print(const Student& st)
{
   cout << st.name << " " << st.fn << " " << st.gradesCount << " " << st.averageGrade << endl;
}
Student createStudent(const char* name, int fn, int gradesCount, double avGrade)
{
   Student obj;

   size_t nameLen = strlen(name);

   obj.name = new char[nameLen + 1];
   strcpy(obj.name, name);

   obj.fn = fn;
   obj.gradesCount = gradesCount;
   obj.averageGrade = avGrade;

   return obj;
}
void saveStudentToFile(ofstream& f, const Student& st)
{
   size_t nameLen = strlen(st.name);
   
   f.write((const char*)&nameLen, sizeof(nameLen));  //first we write the size of the name!
   f.write(st.name, nameLen);
   
   f.write((const char*)&st.fn, sizeof(st.fn));
   f.write((const char*)&st.gradesCount, sizeof(st.gradesCount));
   f.write((const char*)&st.averageGrade, sizeof(st.averageGrade));

}
Student readStudentFromFile(ifstream& f)
{
   Student res;

   size_t nameLen;

   f.read((char *)&nameLen, sizeof(nameLen)); //first we read the size of the name!

   res.name = new char[nameLen + 1];
   f.read(res.name, nameLen);
   res.name[nameLen] = '\0';

   f.read((char *)&res.fn, sizeof(res.fn));
   f.read((char*)&res.gradesCount, sizeof(res.gradesCount));
   f.read((char*)&res.averageGrade, sizeof(res.averageGrade));

   return res;
}
void freeStudent(Student& s)
{
   delete[] s.name;

   s.averageGrade = s.fn = s.gradesCount = 0;

}


int main()
{
   { //save students to file
   	Student s1 = createStudent("Ivan", 1234, 2, 4);
   	Student s2 = createStudent("Petur", 5555, 5, 5.5);

   	ofstream f1("uni.dat", ios::binary);

   	if (!f1.is_open())
   	{
   		cout << "Error" << endl;
   		return -1;
   	}

   	saveStudentToFile(f1, s1);
   	saveStudentToFile(f1, s2);
   	
   	freeStudent(s1);
   	freeStudent(s2);

   	f1.close();
   }

   { //read students from file
   	ifstream f2("uni.dat", ios::binary);

   	if (!f2.is_open())
   	{
   		cout << "Error" << endl;
   		return -1;
   	}
   	Student s1 = readStudentFromFile(f2);
   	Student s2 = readStudentFromFile(f2);

   	print(s1);
   	print(s2);

   	freeStudent(s1);
   	freeStudent(s2);

   	f2.close();
   }
}