Skip to content
Snippets Groups Projects
Commit 6e2b1ade authored by Michael's avatar Michael
Browse files

added useful comments

parent 150dd99b
No related branches found
No related tags found
No related merge requests found
#include "file_own.h" #include "file_own.h"
//V: playerhighscore exists and score.txt exists //V: playerhighscore exists and score.txt exists
//N if playerhighscore < current score; score gets written into score txt. Else score is rewritten into file //N if playerhighscore > current score; playerhighscore is written into corresponding space in score.txt. The rest is rewritten into score.txt (including the theme if playerhighscore < score)
int write_table (enum vehicle theme) { int write_table (enum vehicle theme) {
FILE *my_file = fopen("images/score.txt", "r+"); FILE *my_file = fopen("images/score.txt", "r+");
if (my_file == NULL ) { if (my_file == NULL ) {
printf("File could be opened. file_own\n"); printf("File could be opened. file_own\n");
return -1; return -1;
} }
//Für jede Map ein Score //Für jede Map ein Score, initiiert mit Null
int score_normal = 0; int score_normal = 0;
int score_street = 0; int score_street = 0;
int score_trains = 0; int score_trains = 0;
int score_water = 0; int score_water = 0;
//variable theme //liest die scores aus dem file aus
if (fscanf(my_file,"%d,%d,%d,%d", &score_normal, &score_street, &score_trains, &score_water) == EOF) { if (fscanf(my_file,"%d,%d,%d,%d", &score_normal, &score_street, &score_trains, &score_water) == EOF) {
printf("File could not be scanned. Error: %d file_own\n", errno); // wie ferror() ? printf("File could not be scanned. Error: %d file_own\n", errno); // wie ferror() ?
}; };
...@@ -25,6 +24,7 @@ int write_table (enum vehicle theme) { ...@@ -25,6 +24,7 @@ int write_table (enum vehicle theme) {
//truncate overwrites too much, but doesnt really matter afaik //truncate overwrites too much, but doesnt really matter afaik
if (ftruncate(fileno(my_file), 64) == -1) printf("File couldnt be truncated\n"); if (ftruncate(fileno(my_file), 64) == -1) printf("File couldnt be truncated\n");
// Je nach Fall aktualisieren wir den score, wenn dieser kleiner ist als der playerhighscore
switch(theme) { switch(theme) {
case coin: case coin:
if (playerhighscore > score_normal) { if (playerhighscore > score_normal) {
...@@ -66,6 +66,9 @@ int write_table (enum vehicle theme) { ...@@ -66,6 +66,9 @@ int write_table (enum vehicle theme) {
return 0; return 0;
} }
//V: enum vehicle exists and has 4 parameters (with the right names) for the classes
//N: my_file is the same as before or if it was empty 0,0,0,0
//E: returns the current highscore of the theme given to the function
int read_table (enum vehicle theme) { int read_table (enum vehicle theme) {
FILE *my_file = fopen("images/score.txt", "r"); FILE *my_file = fopen("images/score.txt", "r");
if (my_file == NULL ) { if (my_file == NULL ) {
...@@ -82,11 +85,13 @@ int read_table (enum vehicle theme) { ...@@ -82,11 +85,13 @@ int read_table (enum vehicle theme) {
//Wir schließen es im read modus und öffnen es im write modus //Wir schließen es im read modus und öffnen es im write modus
if (fclose(my_file) == EOF) printf("failed to close file. file_own 2\n"); if (fclose(my_file) == EOF) printf("failed to close file. file_own 2\n");
my_file = fopen("score.txt", "w+"); my_file = fopen("score.txt", "w+");
//Wir wissen, da fscanf gefailed aber fopen funktioniert hat, dass das file leer ist und schreibne deshalb 0,0,0,0 rein
fprintf(my_file, "0,0,0,0"); fprintf(my_file, "0,0,0,0");
if (fscanf(my_file,"%d,%d,%d,%d", &score_normal, &score_street, &score_trains, &score_water) == EOF) printf("Score immer noch nicht drinnen"); if (fscanf(my_file,"%d,%d,%d,%d", &score_normal, &score_street, &score_trains, &score_water) == EOF) printf("Score immer noch nicht drinnen");
}; };
if (fclose(my_file) == EOF) printf("failed to close file. file_own 2\n"); if (fclose(my_file) == EOF) printf("failed to close file. file_own 2\n");
switch (theme) { switch (theme) {
case coin: return score_normal; case coin: return score_normal;
case car: return score_street; case car: return score_street;
...@@ -99,6 +104,7 @@ int read_table (enum vehicle theme) { ...@@ -99,6 +104,7 @@ int read_table (enum vehicle theme) {
//V: score.txt exists //V: score.txt exists
//N: simply makes score.txt empty //N: simply makes score.txt empty
//CURRENTLY NOT USED
int reset_table (void) { int reset_table (void) {
FILE *my_file = fopen("images/score.txt", "w"); FILE *my_file = fopen("images/score.txt", "w");
if (fclose(my_file) == EOF) { if (fclose(my_file) == EOF) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment