Tstream questions

BlitzMax Forums/BlitzMax Beginners Area/Tstream questions

ckob(Posted 2005) [#1]
I am currently porting over alot of Blitz3d code and pretty much moving through the code correcting the errors as it tries to compile. I am stuck on this line:


Result$ = Readfile("Data\Server Data\Script Files\" + Replace$(Params$, Chr$(34), ""))

now when I run it I get an error that says unable to convert a Tstream to a string. Is this something im skipping over or something wrong with bmax?


gman(Posted 2005) [#2]
ReadFile returns a stream object... so:
Local Result:TStream=ReadFile("Data\Server Data\Script Files\" + Replace$(Params$, Chr$(34), ""))

should do it. although you then need to read the string out of the stream. depending on the size of the file, you could do (untested):
Local filestream:TStream=ReadFile("Data\Server Data\Script Files\" + Replace$(Params$, Chr$(34), ""))
Local Result:String=ReadString(filestream,filestream.size())



ckob(Posted 2005) [#3]
so which one should work ? I gave the first one a shot and it didnt work. The second one I couldnt try because I am unsure on how to actually get the size of the file.


Dreamora(Posted 2005) [#4]
There is a command to find out a streams size or you can use EOF (streamname:TSTREAM) ...


Yan(Posted 2005) [#5]
Is there any particular reason the file handle has to be a string?


Dreamora(Posted 2005) [#6]
how do you want to open a file if you don't know its path? :-)


Yan(Posted 2005) [#7]
Eh?

The ReadFile() command (in bb) returns an integer handle, I was asking why ckob wanted it as a string.


gman(Posted 2005) [#8]
@ckob - what is the goal of your line of code? TwoEyedPete is correct in that the original line you posted should have returned a integer file handle. the following is tested code. as long as the file is valid it should work fine:
Strict
Framework BRL.Basic
Import BRL.Retro

Local filename:String="d:/develop/test2.bmx"

' open the file into a stream
Local filestream:TStream=ReadFile(filename)

' somewhere to put the text of the file
Local Result:String

' make sure the stream was opened.  
' filestream will be Null If it failed To open the file.
If filestream
	' read the entire stream as a string and store into a string, using the size method
	' of TStream
	Result=ReadString(filestream,filestream.size())
	
	Rem
	' alternatively, if you dont know the size you can loop through and read it in chunks
	While Not filestream.Eof()
		Result:+ReadString(filestream,1024)
	Wend
	EndRem
	
	' close the file back up
	CloseFile(filestream)
EndIf

' show the results
Print(Result)