More Strings

BlitzMax Forums/BlitzMax Beginners Area/More Strings

(tu) ENAY(Posted 2005) [#1]
Ok, I still don't fully understand Blitzmax strings.
Check out the code:-

Strict
Graphics 640,480

Global fileout = WriteFile("moo.txt")
WriteString(fileout, "ARSE")
CloseFile(fileout)

Global wib:String = "WIBBLE"
Global intget:Int

Global filein = ReadFile("moo.txt")
'intget = Readint(filein)
wib = ReadString(filein, intget)
CloseFile(filein)

Repeat
	Cls
	SetColor 255,255,255
	DrawText "TEST",0,0
	
	If wib = "" Then 
		DrawText "EMPTY", 50, 50
	Else
		DrawText wib, 50, 50
	EndIf
	
	Flip
Until KeyDown(KEY_ESCAPE)

End


If I read the string back, I get nothing at all, and then if I uncomment the line to read the int, the program hangs my machine.


Scott Shaver(Posted 2005) [#2]
There is no int in the file. just the string. You either have to knwo exactly the length of the string you want to read or use ReadLine.

The only other way to do it would be write a binary file with the size of the string in an int and then the bytes of the string. The read them back in int first for the length and then read that number of bytes to construct the string.


(tu) ENAY(Posted 2005) [#3]
I'm trying to read a file of randomly sized strings. If I use readline I just get a whole line of strings.

You need to read the int when you're reading an old skool Blitz string.
I just really can't believe that you can WriteString but can't read it back using it's alter ego command ReadString.

So you're really saying that I have to manually store the size of the string first and then save the string in order to read it back correctly.

Lame.


Scott Shaver(Posted 2005) [#4]
yes. make it easier on your self and create two new functions OldReadString:String(t:TStream) and OldWriteString(s:String,t:TStream).

the write string should write the length and then the string bytes and the read should read the int and then the string bytes.


(tu) ENAY(Posted 2005) [#5]
Yep. Have written these commands. Thanks for the help Scott :)
As I was reading back nothing or getting a machine hang I couldn't really tell what I was storing at first. Sorted!

I can now understand why WarrenM thinks these commands are broken.


Tom(Posted 2005) [#6]
ENAy: in your code you're asking it to read a string of length 0 bytes

Global intget:Int '0

Strict
Graphics 640,480,0

Global fileout = WriteFile("moo.txt")
WriteString(fileout, "ARSE")
CloseFile(fileout)

Global wib:String
Global intget:Int=FileSize("moo.txt")
Global filein = ReadFile("moo.txt")
wib = ReadString(filein, intget)
CloseFile(filein)

Repeat
	Cls
	SetColor 255,255,255
	DrawText "TEST",0,0
	
	If wib = "" Then 
		DrawText "EMPTY", 50, 50
	Else
		DrawText wib, 50, 50
	EndIf
	
	Flip
Until KeyDown(KEY_ESCAPE)
End


Any reason you can't use WriteLine() & ReadLine() instead, then you won't have to store sizes.