string length

BlitzMax Forums/BlitzMax Beginners Area/string length

is there anyway of reading a string in without knowing how long it is, say if its a null terminated string

levelsetname=ReadString$(levdat,5)

this reads in 5 characters


does the readline$ read in all of A NULL terminated string...

I always use WriteLine/ReadLine for strings, as it's a lot easier than having to know in advance how long a string is, as you do with ReadString.

Assuming you've written your data sequentially, you could write a byte/short/int before the string, which tells you how long the string actually is.

If you use WriteStream and ReadStream you have to store a byte before the string.

what i was actually trying to do was read a file i hadn't created its a level file for another(somebody else's game), so all i knew was that it contained null terminated strings in 'c' terms the language. so if you dont know how long the string is because you didnt make it, i would have to use readline, also that then begs if i have to then save this file back out when i have modified it, when blitzmax writes say a writeline, is this classed as a null terminated string to be compatible with the previous one read in?

oh the other thing with strings each character in a string is 8 bytes is that right, and with null terminated charachters at the end is that one character or 8 bytes anyone know.....?

No each character is one byte.

Here is a simple example of how you could handle this sort of thing...
SuperStrict

' create a file containing a c string
Local f:TStream = WriteFile("temp.dat")
WriteCString f,"This is a test"
CloseFile f

' read the file back again
f = ReadFile("temp.dat")
Print ReadCString(f)
CloseFile f

Function WriteCString(file:TStream,txt$)
	WriteString file,txt$
	WriteByte file,0
EndFunction

Function ReadCString:String(file:TStream)
	Local b:Int,txt$
	Repeat
		b=ReadByte(file)
		If b txt$:+Chr(b)
	Until b=0
	Return txt$
EndFunction