-
Notifications
You must be signed in to change notification settings - Fork 0
/
score.c
66 lines (59 loc) · 1.44 KB
/
score.c
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
#include "connect4.h"
void AddPlayer(Player p) {
if ((f = fopen("scoreDatabase.bin", "ab")) == NULL) {
ErrorMessage("Error at opening score database");
}
fwrite(&p, sizeof(p), 1, f);
fclose(f);
}
/* Returns 0 if Player not found
Returns 1 otherwise */
int SearchPlayer(Player p) {
Player temp;
if ((f = fopen("scoreDatabase.bin", "rb")) == NULL) {
ErrorMessage("Error at opening score database");
}
while((fread(&temp, sizeof(temp), 1, f)) == 1) {
if(strcmp(p.name, temp.name) == 0) {
fclose(f);
return 1;
}
}
fclose(f);
return 0;
}
int GetPlayerScore(Player p) {
Player temp;
if ((f = fopen("scoreDatabase.bin", "rb")) == NULL) {
ErrorMessage("Error at opening score database");
}
while((fread(&temp, sizeof(temp), 1, f)) == 1) {
if(strcmp(p.name, temp.name) == 0)
return temp.score;
}
fclose(f);
return 0;
}
void UpdatePlayer(Player p) {
Player temp;
long pos;
if ((f = fopen("scoreDatabase.bin", "rb+")) == NULL) {
ErrorMessage("Error at opening score database");
}
while((fread(&temp, sizeof(temp), 1, f)) == 1)
if(strcmp(temp.name, p.name) == 0) {
pos = ftell(f) - sizeof(temp);
break;
}
fseek(f, pos, 0);
fwrite(&p, sizeof(p), 1, f);
fclose(f);
}
void PrintDatabase() {
f = fopen("scoreDatabase.bin", "rb");
Player p;
while((fread(&p, sizeof(p), 1, f)) == 1) {
printf("%s has %d points\n", p.name, p.score);
}
fclose(f);
}