Easy Way to Write a File?

BlitzMax Forums/BlitzMax Programming/Easy Way to Write a File?

If there any easy way to append to a file without replacing it?

	pTStream=WriteFile("Error.txt")
	
		If not pTStream RuntimeError "Failed to Open the Error.txt File." 
		WriteLine pTStream, sMessage
		CloseStream pTStream


I don't think you can do it with WriteFile, doesn't that mean create a new file?

You can `Open Stream` and set the flags to allow reading and writing.

I tried the stream, but it just keeps replacing the file? Surely, I don't have to read the entire file and append just to do this?

Function WriteErrorToFile(sMessage:String)

	Try
		Local pTStream:TStream = Null

		'Open the Error Log.
		pTStream = OpenStream("Error.txt")
	
		If not pTStream 
			Throw ("Failed to Open the Error.txt File.") 
		End If

		While not Eof(pTStream)
			ReadLine(pTStream)
		Wend


		WriteLine pTStream, sMessage
		CloseStream pTStream
		
    Catch ex:Object   
		Print ex.ToString() 
		End
	End Try   
	
End Function


Local f:TStream = OpenFile("blah.txt")	' open file
SeekStream f,StreamSize(f)				' seek to end of file
WriteLine f,"hello"						' append text
CloseFile f
Something like that?

Thanks