Read/Write Structure

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Read/Write Structure

Post by BackupUser »

Code updated for 5.20+

Restored from previous forum. Originally posted by Berikco.

it seems many ppl, even old PB users dit not know you can save structure at once.
IRC user asked me to post this. :)

Code: Select all

Structure test
  xp.l
  yp.l
  wd.l
  hi.l
EndStructure

fr.test

fr\xp=10
fr\yp=20
fr\wd=500
fr\hi=200

l=SizeOf(test)

If CreateFile(1, "c:\test")
  WriteLong(1, l)
  WriteData(1, @fr,l)
  CloseFile(1)
EndIf

l=0

fr\xp=0
fr\yp=0
fr\wd=0
fr\hi=0

If OpenFile(1,"c:\test")
  l=ReadLong(1)
  ReadData(1, @fr,l)
  CloseFile(1)
EndIf

Debug fr\xp
Debug fr\yp
Debug fr\wd
Debug fr\hi


Regards,

Berikco

http://www.benny.zeb.be
Last edited by fsw on Wed Aug 21, 2013 9:56 pm, edited 1 time in total.
Reason: Code updated for 5.20+
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.

and a bit shorter if SizeOf() goes as constant into the Read/WriteData:

Code: Select all

Structure test   xp.l
   yp.l
   wd.l
   hi.l
 EndStructure
 fr.test
 fr\xp=10
 fr\yp=20
 fr\wd=500
 fr\hi=200
 If CreateFile(1,"test")
   WriteData(@fr,SizeOf(test))
   CloseFile(1)
 EndIf
 fr\xp=0
 fr\yp=0
 fr\wd=0
 fr\hi=0
 If OpenFile(1,"test")
   ReadData(@fr,SizeOf(test))
   CloseFile(1)
 EndIf
 Debug fr\xp
 Debug fr\yp
 Debug fr\wd
 Debug fr\hi

Its a long way to the top if you wanna .....CodeGuru
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Warning, this doesn't work with structure which cointains 'String' fields, as only the pointer will be saved.

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Berikco.
Originally posted by fred

Warning, this doesn't work with structure which cointains 'String' fields, as only the pointer will be saved.

Fred - AlphaSND
Yep, for strings, use bytes.
Not name.S
but Name.b[50]
wich can store a string of 50 bytes


Regards,

Berikco

http://www.benny.zeb.be
Post Reply