File Streams - Appending to File

Blitz3D Forums/Blitz3D Programming/File Streams - Appending to File

_PJ_(Posted 2008) [#1]
I have a simple Logging function for a project, but I can't seem to get it to work correctly. Currently, the code just seems to overwrite a single line each time.

I am not sure if it's related to the closing and re-opening. (Incidentally, what diffeerence(s) are there between OpenFile and WriteFile? ) However, I don't like to leave any files Open, especially as my program may involve other read/writes to other files elsewhere.

Function Debug(sDebugFunction$="APPLICATION RUN",sDebugResult$="LOG START")

	If (DebugOn())
		DebugFile=WriteFile(fLOGFILE$)
		SeekFile(DebugFile,FileSize(fLOGFILE$))
		sLine$=CurrentDate$()+" | "+CurrentTime$()+" | "+sDebugFunction$+" | "+sDebugResult$
		WriteLine(DebugFile,sLine$)
		CloseFile DebugFile
	EndIf
        Return
End Function


Any help would be really appreciated.


GIB3D(Posted 2008) [#2]
Maybe try doing ReadLine to see if there is anything on that line first and loop it till it gets to an empty spot?


Kev(Posted 2008) [#3]
Use OpenFile(), As far as im aware WriteFile() zeros the file ready for writing. You might want to use WriteFile() first to create a file. OpenFile() requires that the file exists.


WolRon(Posted 2008) [#4]
May offer some help:
http://www.blitzbasic.com/Community/posts.php?topic=27896


big10p(Posted 2008) [#5]
Yes, you need to check to see if the log file exists, if it does use OpenFile, else use WriteFile.


_PJ_(Posted 2008) [#6]
Oh the file definitely exists.

OpenFile worked fine. Thanks for the info. I shoulda tried that before posting, really ;)


bytecode77(Posted 2008) [#7]
file = OpenFile(path$)
SeekFile file, FileSize(path$)
The file is now opened for reading and writing and the file position is at the end of the file.