November 25, 2020, 08:31:58 AM

Author Topic: [bb] AddLineToFile by ThePict [ 1+ years ago ]  (Read 568 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] AddLineToFile by ThePict [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : AddLineToFile
Author : ThePict
Posted : 1+ years ago

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

Code :
Code: BlitzBasic
  1. Function AddLineToFile(textline$,file$)
  2. mf3=WriteFile("tmp.txt")
  3. If FileType(file$)=0 Then Goto writelastline
  4. mf2=ReadFile(file$)
  5. Repeat
  6. w$=ReadLine(mf2)
  7. WriteLine(mf3,w$)
  8. Until Eof(mf2)
  9. CloseFile(mf2)
  10. .writelastline
  11. WriteLine(mf3,textline$)
  12. CloseFile(mf3)
  13. CopyFile "tmp.txt",file$
  14. 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 :)
Code: [Select]
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


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal