Saving Objects To A File

BlitzMax Forums/BlitzMax Beginners Area/Saving Objects To A File

z80jim(Posted 2006) [#1]
Is there any way to write and load an object to a file besides writing and reading the individual fields by just writing out the contents of the memory it occupies?Any examples would also be helpful.

Thanks.


FlameDuck(Posted 2006) [#2]
XML is your friend. As is MaXML.


tonyg(Posted 2006) [#3]
z80jim, I'm trying exactly the same thing at the moment.
I have tried...
mynew:Ttest = new Ttest
field blah, blah blah
end type.
local Myptr:byte ptr = byte ptr(mynew)
myfile:tStream=writefile("output.dat")
for count = 0 to sizeof(mynew)
  writebyte(myfile,myptr[count]
next
closefile myfile.

<this is from scratch rather than an actual program>
However, checking the output file using Hex Editor shows some of the characters transposed.
Not sure why yet but it might be the fact I have the wrong field type in my Object (long story).
You might want to try it and see what happens but, it's likely, an instance isn't in contiguous memory.
<edit> Hmmm, might try...
Local mybank:TBank=CreateStaticBank(mynew,SizeOf(mynew))
Local myfile:TStream=WriteStream("decs1.txt")
WriteBank(mybank,myfile,0,SizeOf(mynews))
CloseFile myfile



z80jim(Posted 2006) [#4]
Flameduck, great tip on MaXML. Looking forward to checking it out. Probably doesn't save much work because I'm assuming I still have to feed each field to MaXML. But XML is probably a good fit for my rather large and complex save files.


Brucey(Posted 2006) [#5]
I see TStream has a SaveObject() method that doesn't do anything.
I suppose one could extend TStream for your object and add a customized writer for it...

there's also a LoadObject() method...


grable(Posted 2006) [#6]
Check out the TDataStream object in data.mod, it seems most builtin types have a data type. havent tested this myself though ;)