-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbest_score.c
47 lines (40 loc) · 912 Bytes
/
best_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
#include "main.h"
/**
* read_best_score - Function to read the best score
* from best_score.txt file.
*
* Return: an integer representing the best score
* read from the best_score.txt file.
*/
int read_best_score(void)
{
/* Variable declarations */
int best_score = 0;
FILE *file = fopen("best_score.txt", "r");
if (file != NULL)
{
fscanf(file, "%d", &best_score);
fclose(file);
}
return (best_score);
}
/**
* write_best_score - Function to write the best score to best_score.txt file.
*
* @score: score to be written to the best_score.txt file
* if it's higher than the current best score.
*/
void write_best_score(int score)
{
/* Variable declarations */
int best_score = read_best_score();
if (score > best_score)
{
FILE *file = fopen("best_score.txt", "w");
if (file != NULL)
{
fprintf(file, "%d", score);
fclose(file);
}
}
}