Page 1 of 1

Read/Write Structure

Posted: Wed Oct 23, 2002 11:02 am
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

Posted: Wed Oct 23, 2002 11:38 am
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

Posted: Thu Oct 24, 2002 11:49 am
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

Posted: Thu Oct 24, 2002 11:53 am
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