Code archives/File Utilities/Saving/Loading Types

This code has been declared by its author to be Public Domain code.

Download source code

Saving/Loading Types by BlackJumper
(Posted 22 years ago)
This was created in response to a request about saving and reloading a game. Someone demonstrated a custom save routine + corresponding loader.

To me this seemed like too much work, when the Str$() function will format all fields in a type for printing, thus saving half the work. The challenge then becomes the writing of a parser for the text values of your type.

The example below shows the reading of a couple of ints and a string. There is a code snippet (add's Parser Function) elsewhere in the Archives that could help with parsing other values such as floats...

http://www.blitzbasic.com/codearcs/codearcs.php?code=161
[code]
;------------------------------------------------------------------
;----   demo of saving and reloading game data using Str$()    ----
;----             Blackjumper  - Jan 2005                      ----
;------------------------------------------------------------------

Type test
	Field x
	Field y
	Field name$
End Type

For count = 1 To 4
	n.test = New test
	n\x = Rand(10)
	n\y = Rand(10)+100
	n\name$ = Chr(Rand(26)+65) +Chr(Rand(26)+65) +Chr(Rand(26)+65)
Next


fileout = WriteFile("C:\storedgame.txt")
For n.test = Each test
	Print Str$(n)
	WriteString (fileout, Str$(n))
Next
CloseFile( fileout ) 

Print "game data written to file... press any key to continue"
Print
WaitKey

Print "deleting all instances of type 'test'..."
For n.test = Each test
	Delete n
Next
Print "printing all type information...
Print "________________________________"
For n = Each test
	Print Str$(n)
Next
Print "--------------------------------"
WaitKey
Print


Print "... now reading from disk..."
filein = ReadFile("C:\storedgame.txt") 
While Not Eof(filein)
Read1$ = ReadString$( filein ) 
RestoreTestInfo(Read1$)
Wend

Print "printing all reloaded type information...
Print "------------------------"
For n.test = Each test
	Print Str$(n)
Next
Print "------------------------"

WaitKey
End

Function RestoreTestInfo( SavedString$ )
	Print "Read from file --> " + SavedString$
	SavedString$ = Mid$( SavedString$, 2, Len(SavedString$)-2) ; remove end square brackets
	
	firstcomma = Instr(SavedString$, ",")
	firstvalue% = Left$(SavedString$, firstcomma-1)   ; convert first value (up to comma) to an int
	
	SavedString$ = Mid$( SavedString$, firstcomma+1, Len(SavedString$)-firstcomma+1)  ; eat up to 1st comma

	firstcomma = Instr(SavedString$, ",")
	secondvalue% = Left$(SavedString$, firstcomma-1)  ; convert up to new first comma to another int
	
	SavedString$ = Mid$( SavedString$, firstcomma+1, Len(SavedString$)-firstcomma+1)  ; eat up to new 1st comma
	ThirdString$ = Mid$( SavedString$, 2, Len(SavedString$)-2)                        ; remove quotes from string

	reloaded.test = New test			; make a new type
	reloaded\x = firstvalue%			; and assign the
	reloaded\y = secondvalue%			; reloaded values
	reloaded\name$ = ThirdString$		; to the fields
	
End Function

[/code]