From 27ea0961d2a3f2f5e80966f1506a81215a2f2af1 Mon Sep 17 00:00:00 2001 From: Roman Fomin Date: Tue, 24 Dec 2024 02:13:07 +0700 Subject: [PATCH] Fix -levelstat command (#1249) Close file stream in G_WriteLevelStat function. --- src/doom/g_game.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/doom/g_game.c b/src/doom/g_game.c index 66a81e6ff..6aaaead9d 100644 --- a/src/doom/g_game.c +++ b/src/doom/g_game.c @@ -1985,7 +1985,7 @@ static void G_FormatLevelStatTime(char *str, int tics) // [crispy] Write level statistics upon exit static void G_WriteLevelStat(void) { - static FILE *fstream = NULL; + FILE *fstream; int i, playerKills = 0, playerItems = 0, playerSecrets = 0; @@ -1994,15 +1994,22 @@ static void G_WriteLevelStat(void) char totalTimeString[TIMESTRSIZE]; char *decimal; - if (fstream == NULL) + static boolean firsttime = true; + + if (firsttime) { - fstream = fopen("levelstat.txt", "w"); + firsttime = false; + fstream = M_fopen("levelstat.txt", "w"); + } + else + { + fstream = M_fopen("levelstat.txt", "a"); + } - if (fstream == NULL) - { - fprintf(stderr, "G_WriteLevelStat: Unable to open levelstat.txt for writing!\n"); - return; - } + if (fstream == NULL) + { + fprintf(stderr, "G_WriteLevelStat: Unable to open levelstat.txt for writing!\n"); + return; } if (gamemode == commercial) @@ -2039,6 +2046,8 @@ static void G_WriteLevelStat(void) levelString, (secretexit ? "s" : ""), levelTimeString, totalTimeString, playerKills, totalkills, playerItems, totalitems, playerSecrets, totalsecret); + + fclose(fstream); } void G_DoCompleted (void)