Getting strings from a file using ReadString()?

BlitzMax Forums/BlitzMax Beginners Area/Getting strings from a file using ReadString()?

neos300(Posted 2009) [#1]
How do I do this without knowing the length of the string I am reading?


Ked(Posted 2009) [#2]
ReadLine.


Volker(Posted 2009) [#3]
Method ReadLine$()
Description: Read a line of text from the stream
Information: Bytes are read from the stream until a newline character (ascii code 10) or null character (ascii code 0) is read, or end of file is detected.
Carriage return characters (ascii code 13) are silently ignored.

The bytes read are returned in the form of a string, excluding any terminating newline or null character.


plash(Posted 2009) [#4]
You can use ReadLine (as others before me have said) or make two custom functions: one that writes the length of the string as an integer, then the string; and another function that reads the length of the string, and then the string (based on the length read).


Ked(Posted 2009) [#5]
An example of the custom functions Plash described:
Function BBReadString:String(stream:TStream)
	Local size:Int=ReadInt(stream)
	Return ReadString(stream,size)
EndFunction

Function BBWriteString(stream:TStream,t:String)
	Local size:Int=t.length
	WriteInt(stream,size)
	WriteString(stream,t)
EndFunction

This is the way BlitzPlus, Blitz3D, etc. does it.