so WriteStructure() writes the memory address your structure is at to the currently open file. it tests to see whether the file exists before doing so. it does NOT open the file for you. the procedure will return 0 if it fails, otherwise it will return the location you are now in the file after writing the structure.
read structure follows the same pattern.
Code: Select all
;- Procedures
Procedure WriteStructure(file,location.l,*struct,sizeofstruct.l)
proceed=IsFile(file)
If proceed
FileSeek(location)
WriteData(*struct,sizeofstruct)
returnval=Loc()
ProcedureReturn returnval
Else
ProcedureReturn 0
EndIf
EndProcedure
Procedure ReadStructure(file,location,*struct, sizeofstruct.l)
proceed=IsFile(file)
If proceed
FileSeek(location)
ReadData(*struct,sizeofstruct)
returnval=Loc()
ProcedureReturn returnval
Else
ProcedureReturn 0
EndIf
EndProcedure
;- Structures
Structure test
long.l
string.s
;quad.q
float.f
;double.d
word.w
array.l[30]
array2.s[30]
EndStructure
;-Setup Our Variables
*var.test=HeapAlloc_(GetProcessHeap_(), 0, SizeOf(test))
*var\long=34343434
*var\string="hello"
*var\float=0.54545
*var\array[20]=444
*var\array2[20]="Huh?"
*var\word=77
;- Main code
If OpenWindow(0, 100, 200, 350, 350, #PB_Window_MinimizeGadget, "")
If CreateGadgetList(WindowID(0))
ButtonGadget(1,50,300,50,20,"start")
EndIf
Repeat
EventID = WaitWindowEvent()
If EventID = #PB_EventGadget
Select EventGadgetID()
Case 1
If EventType()=#PB_EventType_LeftClick
If CreateFile(0,"C:\test.stc")
Debug WriteStructure(0,0,*var,SizeOf(test))
EndIf
CloseFile(0)
If OpenFile(5,"C:\test.stc")
*newvar.test=HeapAlloc_(GetProcessHeap_(), 0, SizeOf(test))
Debug ReadStructure(5,0,*newvar,SizeOf(test))
Debug *newvar\long
Debug *newvar\string
Debug *newvar\array[20]
Debug *newvar\float
Debug *newvar\array2[20]
Debug *newvar\word
CloseFile(5)
EndIf
EndIf
EndSelect
EndIf
Until EventID = #PB_EventCloseWindow
EndIf
End