Read file question

Blitz3D Forums/Blitz3D Programming/Read file question

Farflame(Posted 2005) [#1]
I'm saving my file as 512 individual bytes using Writebyte, but is it possible to read the entire file back as a single string? If I just did a ReadString of my file would it get a 512 character string back? Or do I need to read it back byte-by-byte?


Baystep Productions(Posted 2005) [#2]
I think that requires the string format, of an intreger of the length, and then the actual sting. Maybe a closure charecter like17 or sumtin.

Check under the commands list under write string. That should help maybe.


Baystep Productions(Posted 2005) [#3]
Here it is!

"Each string is stored in the file as a 4 byte (32bit) integer followed by the characters that form the string. The integer contains the number of characters in the string, i.e. its length. Note, that Carriage Return, Line Feed and Null characters are NOT use to indicate the end of the string. A file of strings cannot be read like a text file, since it contains string variables and not text. A null string, i.e. a string of zero length ("") is stored as 4 bytes, an integer count with a value = zero, followed by no Characters. Note strings are not limited to 255 characters as in some languages. Reading beyond the end of file does not result in an error, but each value read will be a zero length string."


Graythe(Posted 2005) [#4]
So - What you need to do is something like;

WriteInt StreamHandle,Len(StreamData$)

before writing the actual data byte by byte.

Then when you want to read data the readstring() operation will read the leading enumerator and return the number of characters specified.


Farflame(Posted 2005) [#5]
Unfortunately, I NEED to write the data byte by byte, so I think I have to read it byte by byte aswell. It's ok, it's just that it would've been easier to read it all in as a single string, but I've just done a byte-by-byte read afterall and it works fine.


Banshee(Posted 2005) [#6]
Following on from PCD Guy's post yes you can read it in as 1 512 byte block. If I understand his post correctly:

If you wrote an extra 4 byte header out first containing the length of the string in integer format ( writeInt stream,512 ) you should then be able to use readString to get the data back in.