Read/Write Structures

Share your advanced PureBasic knowledge/code with the community.
localmotion34
Enthusiast
Enthusiast
Posts: 665
Joined: Fri Sep 12, 2003 10:40 pm
Location: Tallahassee, Florida

Read/Write Structures

Post 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 

Code: Select all

!.WHILE status != dwPassedOut
! Invoke AllocateDrink, dwBeerAmount
!MOV Mug, Beer
!Invoke Drink, Mug, dwBeerAmount
!.endw
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1285
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post 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_()
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

The heap is faster, Paul.
! Black holes are where God divided by zero !
My little blog!
(Not for the faint hearted!)
Post Reply