-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDbRepository.cpp
57 lines (52 loc) · 1.38 KB
/
DbRepository.cpp
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
#include "pch.h"
#include "IRepository.h"
#include "Student.h"
class DbRepository: public IRepository {
private:
fstream dbFile;
public:
bool Create(Student* student) {
dbFile.open("StudentsGrades.db", ios::app | ios::binary);
if (!dbFile) {
cerr << "File couldn’t be opened!\n";
return false;
}
long long fn = student->getFN();
dbFile.write((char*)&fn, sizeof(long long));
dbFile.write(student->getFirstName(), 256);
dbFile.write(student->getLastName(), 256);
short grade = student->getGrade();
dbFile.write((char*)&grade, sizeof(short));
dbFile.close();
cout << "Record saved!" << endl;
return true;
}
virtual bool Delete(long long id) {
return 0;
}
virtual void Read(long long id) {
dbFile.open("StudentsGrades.db", ios::in | ios::binary);
if (!dbFile) {
cout << "File couldn’t be opened!\n";
}
dbFile.seekg(0, ios::end);
long end = dbFile.tellg();
dbFile.seekg(0, ios::beg);
for (long g = 0; g < end; g = dbFile.tellg()) {
long long temp;
dbFile.read((char*)&temp, sizeof(long long));
if (temp == id) {
char fname[256];
dbFile.read((char*)&fname, 256);
char lname[256];
dbFile.read((char*)&lname, 256);
cout << temp << " " << fname << " " << lname << endl;
dbFile.close();
return;
}
else dbFile.seekp(512 + sizeof(short), ios::cur);
}
dbFile.close();
cout <<"Record not found!" << endl;
}
};