Skip to content

Commit

Permalink
Fix CSV header being written multiple times to ddnet-saves.txt
Browse files Browse the repository at this point in the history
It is implementation-defined whether the file position returned by `ftell` denotes the beginning or the end of the file when opening in append-mode. This was causing the condition `io_tell(File) == 0` to always be true, so the CSV header was also written to `ddnet-saves.txt` every time that a new save code is written. Now, we check if the file already exists before appending and only write the CSV header once when the file does not exist yet.
  • Loading branch information
Robyt3 committed Jul 25, 2024
1 parent cd81b1f commit 5e65561
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/game/client/components/chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ void CChat::StoreSave(const char *pText)
}
*/

const bool SavesFileExists = Storage()->FileExists(SAVES_FILE, IStorage::TYPE_SAVE);
IOHANDLE File = Storage()->OpenFile(SAVES_FILE, IOFLAG_APPEND, IStorage::TYPE_SAVE);
if(!File)
return;
Expand All @@ -615,7 +616,7 @@ void CChat::StoreSave(const char *pText)
aSaveCode,
};

if(io_tell(File) == 0)
if(!SavesFileExists)
{
CsvWrite(File, 4, SAVES_HEADER);
}
Expand Down

0 comments on commit 5e65561

Please sign in to comment.