For some reason, there always ended up being a growing number of line breaks at the end of the log.
I tried fixing it by limiting how far it goes when pushing data to the file: (notice the -1)
for a = 1 to log$.length-1
But this is a messy solution which delete data if the last line fail to have a linebreak.
// Project: test-array
// Created: 2019-11-28
SetErrorMode( 2 )
SetWindowTitle( "test-array" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 )
SetVirtualResolution( 1024, 768 )
SetVSync( 1 )
UseNewDefaultFonts( 1 )
Global saveDir$ as String = "AGK_LOGFILE"
do
if GetRawKeyPressed(74) rem on pressing J
print ("J is pressed")
log_add("Hi! Nice day!")
endif
Sync()
loop
rem this function is called each time a sprint has been finished
function log_add(new$ as string )
logFile as Integer
log$ as String[0]
rem if the file doesn't exits, it must be created
if GetFileExists( "raw:" + GetDocumentsPath () + "\" + saveDir$ + "\log.txt" ) = 0
MakeFolder("raw:" + GetDocumentsPath () + "\" + saveDir$)
logFile = OpenToWrite( "raw:" + GetDocumentsPath () + "\" + saveDir$ + "\log.txt")
WriteLine(logFile, chr(10) )
Closefile(logFile)
endif
rem load file
logFile = OpenToRead( "raw:" + GetDocumentsPath () + "\" + saveDir$ + "\log.txt")
rem add new entry
log$.insert(new$ )
rem add old entries
repeat
log$.insert(ReadLine( logFile ))
until FileEOF(logFile) = 1
Closefile(logFile)
logFile = OpenToWrite( "raw:" + GetDocumentsPath () + "\" + saveDir$ + "\log.txt")
a as integer
for a = 1 to log$.length-1
WriteLine(logFile, log$[a])
next
Closefile(logFile)
endfunction