Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 04 / uni_task(not finished).cpp
blob: 7bf3a70e364f3e633dc3dc631dc6f2a156972bb6 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#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;

}

const int UNI_NAME_MAX = 32;
struct Uni
{
	char name[UNI_NAME_MAX];
	size_t studentsCount;
	Student* students;
};

void saveUniToFile(ofstream& f, const Uni& uni)
{
	
	f.write(uni.name, sizeof(uni.name));
	f.write((const char*)&uni.studentsCount, sizeof(uni.studentsCount));  //first we write how many students we have!
	
	for (int i = 0; i < uni.studentsCount; i++)
		saveStudentToFile(f, uni.students[i]);
}

void readUniFromFile(ifstream& f, Uni& uni)
{
	f.read(uni.name, sizeof(uni.name));
	f.read((char*) &uni.studentsCount, sizeof(uni.studentsCount));  //we read how many students are saved in the file!

	uni.students = new Student[uni.studentsCount];
	
	for (int i = 0; i < uni.studentsCount; i++)
		uni.students[i] = readStudentFromFile(f);
}
void freeUni(Uni& uni)
{
	for (int i = 0; i < uni.studentsCount; i++)
		freeStudent(uni.students[i]);
	strcpy(uni.name, "");
	uni.studentsCount = 0;
}
void print(const Uni&  u)
{
	cout << u.name << " " << u.studentsCount << endl;
	for (int i = 0; i < u.studentsCount; i++)
		print(u.students[i]);
}
int main()
{
	{ //Creating an university and saving to file
		Uni u;
		strcpy(u.name, "FMI");
		u.studentsCount = 3;

		u.students = new Student[u.studentsCount];
		u.students[0] = createStudent("Ivan", 81234, 3, 4.5);
		u.students[1] = createStudent("Angel", 12345, 4, 5);
		u.students[2] = createStudent("Mariq", 81234, 5, 5.5);


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

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

		saveUniToFile(f, u);
		freeUni(u);
	}

	{ //Reading an university from file
		
		Uni myUni;
		ifstream f("uni.dat", ios::binary);

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

		readUniFromFile(f, myUni);

		print(myUni);

		f.close();

		freeUni(myUni);

	}
}