ReadString nonsense

BlitzMax Forums/BlitzMax Beginners Area/ReadString nonsense

(tu) ENAY(Posted 2005) [#1]
I just don't get ReadString in Blitzmax.

In Blitz 3D/2D you did :-

ReadString(f$)

But now in Blitzmax you have to specify the size as well.

ReadString(f$,<stringsize>)


What's that all about?

I've got a load of oldskool Blitz strings I want to read in Blitzmax and I can't because I need to know the size. But that's an anomoly in itself because I don't know the sizes of any of the strings until I've read them and I need to know the size before I can read them. WTF! DOH!

AAAAAAAAAAAARRRGGGGGGGHHHHHHH!!! HELP! ;)


boomboommax(Posted 2005) [#2]
fileout = WriteFile("test.txt")
	WriteString(fileout,"pies")
CloseFile(fileout)

filein = ReadFile("test.txt")
	tmpsize = Readint(filein)
	Print ReadString(filein,tmpsize)
CloseFile(filein)


fixed :P


Beaker(Posted 2005) [#3]
That won't work (for anything but strings that are 4 characters long).


Beaker(Posted 2005) [#4]
Try:
Function Old_ReadString(file)
Local size = Readint(file)
Return ReadString(file,size)
End Function


(tu) ENAY(Posted 2005) [#5]
I'll give it a whirl. Cheers dude.


Warren(Posted 2005) [#6]
Yeah, it threw me at first too. I have no idea why they'd butcher it like that, but whatever...

I wrote some wrapper functions for readstring/writestring and moved on.


Scott Shaver(Posted 2005) [#7]
While Not eof(configFile)
Local line:String = readline(configFile).Trim()
...
wend


Warren(Posted 2005) [#8]
... yeah, thanks.


(tu) ENAY(Posted 2005) [#9]
Yep it works. Thanks :)


Warren(Posted 2005) [#10]
Yeah, obviously it works. I can read the documentation too.

However that doesn't explain why writestring/readstring are broken. There's still a problem there.


rdodson41(Posted 2005) [#11]
They're not broken. On Unix, strings are stored as an array of bytes followed by a null character, zero. So the string "ABC" would be bytes 65 66 67 0. On windows strings are stored by a size followed by the string. So the string "ABC" would be bytes 3 0 0 0 65 66 67. ( I think the size is 4 bytes long but im not sure. ) Anyway, C obviously uses the Unix format, and all BMX programs are built around imported C code, so they probably follow standards of C.

[EDIT] Also a lot of C functions require you to put in a size. For example, read() takes 3 arguments: file descriptor, buffer pointer, and number of bytes to read.


Warren(Posted 2005) [#12]
They are "broken" in the sense that they don't work like their Blitz3D equivalents (and hence everyone who upgrades from Blitz3D has to stumble over this bump in the road), not that they are actually broken in terms of code.


rdodson41(Posted 2005) [#13]
They're not broken. They're just different. If you look at WriteBytes and ReadBytes they're the same way. Just use the function that Beaker posted.


Warren(Posted 2005) [#14]
I swear, it's like talking to a wall.

They don't work like their Blitz3D counterparts. This is confusing for anyone (ANYONE) moving up to BlitzMax from Blitz3D. Hence, they appear broken.

I'm not sure what part of that is confusing for you and, frankly, I'm done trying to explain it to you.


(tu) ENAY(Posted 2005) [#15]
What I don't like is that you write a string and what you do instead is write an int and then an old skool string when quite clearly you haven't done that in code yourself. (Writing an int first)

What would've helped for me is if the Documentation explained what the size parameter was used for as currently it doesn't tell you anything, even a compilable of source code would've been nice. As when you convert your code from old skool to Blitzmax this totally baffles you considering you didn't need it before.

A tricky one if you're not expecting it.


skidracer(Posted 2005) [#16]
I hear you ENAY, will update the docs.