Readstring()

BlitzMax Forums/BlitzMax Beginners Area/Readstring()

D4NM4N(Posted 2006) [#1]
Im trying to read a file created in blitz using writestring (and others) and this seems impossible as i need to specify the size of the string to be read?!?

Im guessing theres a few bytes before the string to tell you how big?
if so what is the type of the predata of a string in a file to tell how long the string is? Byte? Word? int?

ie
stlen=read????(file)
mystring=readstring(file,stlen)


skidracer(Posted 2006) [#2]
No, WriteString just outputs every character in the string to the stream.

If you want to read an entire textfile in one hit use the FileSize command to get the length of file for the ReadString command.


tonyg(Posted 2006) [#3]
readstring nonsense
for whichI don't think the doc was updated 'cos it's still rubbish.


FlameDuck(Posted 2006) [#4]
Im trying to read a file created in blitz using writestring (and others) and this seems impossible as i need to specify the size of the string to be read?!?
Or you could use WriteLine / ReadLine.


D4NM4N(Posted 2006) [#5]
no i cant use any of them,

Basically its a normal random access file created by blitz3d

ie:

writeint file,284028409
writestring file,"fjsdhfs"
writebyte file,123
etc.etc.etc

Surely theres a way to simply read a string from a file without knowing its actual length!!! if not im gonna be real dissapointed with bmax!


tonyg(Posted 2006) [#6]
You might need to show your code and the file.
Using the WriteString example from B3D...
; Reading and writing a file using ReadString$ and WriteString functions

; Initialise some variables for the example
String1$ = "A short string"
String2$ = "A longer string since these are variables lengths"
String3$ = "This is string3 "
String4$ = "joined to string4"

; Open a file to write to
fileout = WriteFile("mydata.dat")

; Write the information to the file
WriteString( fileout, String1 )
WriteString( fileout, String2 )
WriteString( fileout, String3 + String4)
WriteString( fileout, "Just to show you don't have to use variables" )

; Close the file
CloseFile( fileout )

and then this in Bmax...
mystream:TStream=OpenStream("mydata.dat")
While Not Eof(mystream)
	Print ReadLine(mystream)
Wend

works OK.


D4NM4N(Posted 2006) [#7]
Unfortunately that would be ok if it was all text but what about reading out my integer and byte values as well?


FlameDuck(Posted 2006) [#8]
Surely theres a way to simply read a string from a file without knowing its actual length!!! if not im gonna be real dissapointed with bmax!
No, not just in BlitzMAX, but in general. There are two ways to represent a string in programming languages. In general, these methods all involve a starting address and one of the following:

- A fixed size string (one which has 'n' length, regardless of how many characters it actually has). This requires no additional data.

- An 'n' length string, which needs to know the exact length of a string. This requires you to store or know the exact length of the string (which seems like the case you're struggling with and what ReadString/WriteString relies on).

- A terminated string, which has some sort of terminating character to indicate the end of the string. Traditionally these are 'null terminated' by the ASCII value '0' in programming languages, or CR and/or LF characeters in plain ASCII text files (and what ReadLine/WriteLine does).

Unfortunately that would be ok if it was all text but what about reading out my integer and byte values as well?
Make sure you read it in the same sequence as you write it.


skidracer(Posted 2006) [#9]
Sorry for not reading your original post more carefully.

Surely theres a way to simply read a string from a file without knowing its actual length!!! if not im gonna be real dissapointed with bmax!


As discussed in the other thread you just need to roll your own StringIO functions that perform the same as b3d:
Function ReadBBString$(stream:TStream)
	Local bytes=stream.ReadInt()
	If bytes Return stream.ReadString(bytes)
End Function

Function WriteBBString(stream:TStream,text$)
	stream.WriteInt text.length
	stream.WriteString text
End Function



D4NM4N(Posted 2006) [#10]
ok thanks,

trouble is, the file is old and i havnt got the code that wrote it, i only know the file structure.
How does blitz3d darkbasic etc know what to read? are all strings written of length 255? or something.
I tried reading in an int first and got a mav.

ill keep fiddling
cheers


Dreamora(Posted 2006) [#11]
There is no problem in reading a string.

Read it per byte until you strugle over byte 0 which is the string terminator. Then everything you have read since the beginning of the string till now wanted string :)

(-> while char > 0 string = string + char, char = readbyte() wend)


Michael Reitzenstein(Posted 2006) [#12]
That's not consistant with Blitz strings which can contain the null terminator, and it won't work with Blitz2D/3D/Plus written files. Those Read/Write BB strings with skid posted are much better.