Easy Way to Write a File?

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

Sean Doherty(Posted 2006) [#1]
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



ImaginaryHuman(Posted 2006) [#2]
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.


Sean Doherty(Posted 2006) [#3]
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



REDi(Posted 2006) [#4]
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?


Sean Doherty(Posted 2006) [#5]
Thanks