[bb] AddLineToFile by ThePict [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:38

Previous topic - Next topic

BlitzBot

Title : AddLineToFile
Author : ThePict
Posted : 1+ years ago

Description : Use it to keep a running record of game progress.

Code :
Code (blitzbasic) Select
Function AddLineToFile(textline$,file$)
mf3=WriteFile("tmp.txt")
If FileType(file$)=0 Then Goto writelastline
mf2=ReadFile(file$)
Repeat
w$=ReadLine(mf2)
WriteLine(mf3,w$)
Until Eof(mf2)
CloseFile(mf2)
.writelastline
WriteLine(mf3,textline$)
CloseFile(mf3)
CopyFile "tmp.txt",file$
End Function


Comments :


_PJ_(Posted 1+ years ago)

 This should be a lot quicker,since it jumps to the end of the file and appends a line to it, without  having to copy the entire content.It also negates the reqwuirement for the rather unwieldy "Goto" and Label within a function :)
Function AddLineToFile(textline$,file$)
   mf3=OpenFile(file)
   If (Not(mf3))
      mf3=WriteFile(File)
   End If
   If (Not(mf3))
      DebugLog("Could not write to File: "+file)
   End If  
   SeekFile(mf3,FileSize(file$)-1)
   WriteLine mf3,textline
   CloseFile mf3
End Function