Page 1 of 1

Read/Write Structures

Posted: Mon Feb 20, 2006 8:28 pm
by localmotion34
So i posted about this being a feature, but turns out this wasnt too hard to do with some tweaking.

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 

Posted: Mon Feb 20, 2006 9:39 pm
by Paul
I think if you would have seperated your read and write routines into 2 different buttons, you would see that this doesn't actually work.

Try saving your data (test.stc) to disk.
Rem out the parts in your code that assign data to the structure and write the structure to disk. Use only the routine that reads back your data. You will see much of your data is no good... specifically string data ;)


Also not sure why you are using HeapAlloc_()

Posted: Sun Mar 04, 2007 12:45 am
by dagcrack
The heap is faster, Paul.