what type for file input ?

BlitzMax Forums/BlitzMax Beginners Area/what type for file input ?

DannyD(Posted 2005) [#1]
The following reads from a file and outputs the contents to the console.
[codebox]
file=OpenFile("race.bmx")
If Not file RuntimeError "could not open file openfile.bmx"

While Not KeyHit(Key_escape)
Cls

While Not Eof(file)
print ReadLine(file)
Wend
[codebox]

I however want to output the contents the a display using drawtext.I can't use drawtext readline(file),x,y.I need to store the contents of the file in a type but what type? Will a string type hold eofs and multiple lines ? Thanks in advance


WendellM(Posted 2005) [#2]
It's possible to read an entire file as a single string with "Text$ = ReadString( InFile, StreamSize( InFile ) )" and Print would output it correctly including tabs and returns, but DrawText won't.

This has no wrapping or scrolling for large lines/files, but shows the approach that I'd use:
Strict

Local Filename$ = "race.bmx"
Local InFile:TStream = ReadFile( Filename )
If InFile = Null Then RuntimeError "Could not open file " + Filename

Graphics 1024, 768

Local Text$, TextY
While Not Eof( InFile )
	Text = ReadLine( InFile )
	Text = Replace( Text, Chr(9), "  " ) 'replace tabs with 2 spaces
	DrawText Text, 0, TextY
	TextY :+ TextHeight( Text )
Wend

Flip
CloseFile( InFile )
FlushMem
WaitKey