diff --git a/file_own.c b/file_own.c index b5c2c36633a6e0f5bb80080d91830068b9d96224..3ef3ac91a2bb73333388ee7f9d106c53424cfed6 100644 --- a/file_own.c +++ b/file_own.c @@ -1,21 +1,20 @@ #include "file_own.h" //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) { FILE *my_file = fopen("images/score.txt", "r+"); if (my_file == NULL ) { printf("File could be opened. file_own\n"); return -1; } - //Für jede Map ein Score + //Für jede Map ein Score, initiiert mit Null int score_normal = 0; int score_street = 0; int score_trains = 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) { printf("File could not be scanned. Error: %d file_own\n", errno); // wie ferror() ? }; @@ -25,6 +24,7 @@ int write_table (enum vehicle theme) { //truncate overwrites too much, but doesnt really matter afaik 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) { case coin: if (playerhighscore > score_normal) { @@ -66,6 +66,9 @@ int write_table (enum vehicle theme) { 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) { FILE *my_file = fopen("images/score.txt", "r"); if (my_file == NULL ) { @@ -82,11 +85,13 @@ int read_table (enum vehicle theme) { //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"); 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"); 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"); + switch (theme) { case coin: return score_normal; case car: return score_street; @@ -99,6 +104,7 @@ int read_table (enum vehicle theme) { //V: score.txt exists //N: simply makes score.txt empty +//CURRENTLY NOT USED int reset_table (void) { FILE *my_file = fopen("images/score.txt", "w"); if (fclose(my_file) == EOF) {