Saving Objects To A File
BlitzMax Forums/BlitzMax Beginners Area/Saving Objects To A File
| ||
| 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. |
| ||
| XML is your friend. As is MaXML. |
| ||
| 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
|
| ||
| 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. |
| ||
| 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... |
| ||
| Check out the TDataStream object in data.mod, it seems most builtin types have a data type. havent tested this myself though ;) |