Page 1 of 1
Saving Structures to disk and reading structures from Disk
Posted: Sat Dec 06, 2008 6:00 pm
by Zooker
VB6 won't work in Vista Purebasic seems to. I have a program I really need written in Basic PDS7.11 It has this Type end type lie this:
Type Check
ID as Long
Dayt as Long
Dec as string * 24
Memo as string * 24
Category as string * 2
amount as Currency
Balance as Currency
end Type
I think I can convert this to structures.(I'm not sure how I can handle the currency thing.) Worse, I can't for the life of me figure out how how to save each structure to disk and later retreive each structure from disk. Could some one write an short example of how that might be accomplished, Please?

Posted: Sun Dec 07, 2008 4:49 am
by Sparkie
Is this what you are looking for
Code: Select all
;- Create Check Structure
Structure Check
id.l
Dayt.l
Dec.s{24}
Memo.s{24}
Category.s{2}
Amount.d
Balance.d
EndStructure
;- Fill the structure
InfoWrite.Check
InfoWrite\id = 101
InfoWrite\Dayt = 1228603536
InfoWrite\Dec = "1234567890-1234567890123"
InfoWrite\Memo = "This is a Zooker memo"
InfoWrite\Category = "PB"
InfoWrite\Amount = 1234567.43
InfoWrite\Balance = 9876543.12
;- Write structure data to file
CreateFile(0, "c:\zooker.dat")
WriteLong(0, InfoWrite\id)
WriteLong(0, InfoWrite\Dayt)
WriteString(0, InfoWrite\Dec + #CRLF$)
WriteString(0, InfoWrite\Memo + #CRLF$)
WriteString(0, InfoWrite\Category + #CRLF$)
WriteDouble(0, InfoWrite\Amount)
WriteDouble(0, InfoWrite\Balance)
CloseFile(0)
;- Read structure data from file
ReadFile(0, "c:\zooker.dat")
InfoRead.Check
InfoRead\id = ReadLong(0)
InfoRead\Dayt = ReadLong(0)
InfoRead\Dec = ReadString(0)
InfoRead\Memo = ReadString(0)
InfoRead\Category = ReadString(0)
InfoRead\Amount = ReadDouble(0)
InfoRead\Balance = ReadDouble(0)
CloseFile(0)
DeleteFile("c:\zooker.dat")
;- Display data
info$ = "ID = " + Str(InfoRead\id) + #CRLF$
info$ + "Dayt = " + Str(InfoRead\Dayt) + #CRLF$
info$ + "Dec = " + InfoRead\Dec + #CRLF$
info$ + "Memo = " + InfoRead\Memo + #CRLF$
info$ + "Category = " + InfoRead\Category + #CRLF$
info$ + "Amount = " + StrD(InfoRead\Amount, 2) + #CRLF$
info$ + "Balance = " + StrD(InfoRead\Balance, 2) + #CRLF$
MessageRequester("Check Structure Info", info$, #PB_MessageRequester_Ok | #MB_ICONINFORMATION)
Posted: Sun Dec 07, 2008 6:32 am
by Hroudtwolf
Hi,
There is a little bit easier way.
Code: Select all
Structure tData
sName .s { 32 }
sNickname.s { 32 }
lAge .l
EndStructure
Define.i idFile
Define.tData MyDatas
; Reading
idFile = ReadFile ( #PB_Any , "yourfile.dat" )
If Not idFile
Debug "Can't read file."
End
EndIf
ReadData ( idFile , MyDatas , SizeOf ( tData ) )
CloseFile ( idFile )
; Writting
MyDatas\sName = "Olaf Hansen"
MyDatas\sNickname = "Oha"
MyDatas\lAge = 54
idFile = CreateFile ( #PB_Any , "yourfile.dat" )
If Not idFile
Debug "Can't create file."
End
EndIf
WriteData ( idFile , MyDatas , SizeOf ( tData ) )
CloseFile ( idFile )
Regards
Wolf
Posted: Sun Dec 07, 2008 2:40 pm
by Sparkie
As Hroudtwolf pointed out, you can of course write/read all the data at once by using Write/Read data. Pick the method that best suits your needs.
Code: Select all
;- Create Check Structure
Structure Check
id.l
Dayt.l
Dec.s{24}
Memo.s{24}
Category.s{2}
Amount.d
Balance.d
EndStructure
;- Fill the structure
InfoWrite.Check
InfoWrite\id = 101
InfoWrite\Dayt = 1228603536
InfoWrite\Dec = "1234567890-1234567890123"
InfoWrite\Memo = "This is a Zooker memo"
InfoWrite\Category = "PB"
InfoWrite\Amount = 1234567.43
InfoWrite\Balance = 9876543.12
;- Write structure data to file
CreateFile(0, "c:\zooker.dat")
WriteData(0, @InfoWrite, SizeOf(InfoWrite))
CloseFile(0)
;- Read structure data from file
ReadFile(0, "c:\zooker.dat")
InfoRead.Check
ReadData(0, @InfoRead, SizeOf(InfoRead))
CloseFile(0)
DeleteFile("c:\zooker.dat")
;- Display data
info$ = "ID = " + Str(InfoRead\id) + #CRLF$
info$ + "Dayt = " + Str(InfoRead\Dayt) + #CRLF$
info$ + "Dec = " + InfoRead\Dec + #CRLF$
info$ + "Memo = " + InfoRead\Memo + #CRLF$
info$ + "Category = " + InfoRead\Category + #CRLF$
info$ + "Amount = " + StrD(InfoRead\Amount, 2) + #CRLF$
info$ + "Balance = " + StrD(InfoRead\Balance, 2) + #CRLF$
MessageRequester("Check Structure Info", info$, #PB_MessageRequester_Ok | #MB_ICONINFORMATION)
Posted: Sun Dec 07, 2008 3:12 pm
by Kaeru Gaman
for "Currency" you do not need to use Double,
you could also use Long or Quad and write your own conversion routines to add the comma in display.
This is called Fixcomma, I would prefer using this for Currency values.
if you need to convert existing recordsets...
I do not know wich Type would convert correctly, how VB currency is encoded.
Posted: Sun Dec 07, 2008 3:48 pm
by SFSxOI
does this mean and individual file for each structure?
Posted: Sun Dec 07, 2008 4:01 pm
by gnasen
you can store as much as you want in the file. Just be careful by reading it out. If you store 5 times the structure strucA, you have to read 5 times the SizeOf(strucA) from the file.
Dont mess up your stored data or your app will crash/error !
Posted: Sun Dec 07, 2008 6:09 pm
by Sparkie
FileSeek is your friend.
Code: Select all
;- Create Check Structure
Structure Check
id.l
Dayt.l
Dec.s{24}
Memo.s{24}
Category.s{2}
Amount.d
Balance.d
EndStructure
checkInfo.Check
CreateFile(0, "c:\zooker.dat")
For c = 1 To 5
checkInfo\id = c
checkInfo\Dayt = Date() + c
checkInfo\Dec = "Dec# " + Str(c)
Select c
Case 0
checkInfo\Memo = "This is a Zooker memo."
Case 1
checkInfo\Memo = "The maximum length is 24"
Case 2
checkInfo\Memo = "This is a memo too."
Case 3
checkInfo\Memo = "PureBasic"
Case 4
checkInfo\Memo = "Was Here!"
Case 5
checkInfo\Memo = "The End."
EndSelect
checkInfo\Category = "P " + Str(c)
checkInfo\Amount = 123.43 * c
checkInfo\Balance = 987.12 * c
WriteData(0, @checkInfo, SizeOf(CheckInfo))
Next
CloseFile(0)
ReadFile(0, "c:\zooker.dat")
;- Read all data 0 thru 5
Debug "Here is all of the data..."
Debug ""
For c = 1 To 5
FileSeek(0, SizeOf(Check) * (c - 1))
ReadData(0, @checkInfo, SizeOf(CheckInfo))
Debug "ID = " + Str(checkInfo\id)
Debug "Dayt = " + Str(checkInfo\Dayt)
Debug "Dec = " + checkInfo\Dec
Debug "Meno = " + checkInfo\Memo
Debug "Category = " + checkInfo\Category
Debug "Amount = " + StrD(checkInfo\Amount, 2)
Debug "Balance = " + StrD(checkInfo\Balance, 2)
Debug "========"
Next
;- Read only the 3rd Check entry
entryToRead = 3
Debug ""
Debug "Here is the data for the 3rd entry only..."
Debug ""
;- Move file pointer to 3rd entry
FileSeek(0, SizeOf(CheckInfo) * (entryToRead - 1))
ReadData(0, @checkInfo, SizeOf(CheckInfo))
Debug "ID = " + Str(checkInfo\id)
Debug "Dayt = " + Str(checkInfo\Dayt)
Debug "Dec = " + checkInfo\Dec
Debug "Meno = " + checkInfo\Memo
Debug "Category = " + checkInfo\Category
Debug "Amount = " + StrD(checkInfo\Amount, 2)
Debug "Balance = " + StrD(checkInfo\Balance, 2)
Debug "========"
CloseFile(0)
;- Make changes to 4th entry
OpenFile(0, "c:\zooker.dat")
checkInfo\id = 111
checkInfo\Dayt = 111
checkInfo\Dec = "Dec# 111"
checkInfo\Memo = "Changed Memo 4"
checkInfo\Category = "P" + Str(c)
checkInfo\Amount = 111.11 * c
checkInfo\Balance = 444.44 * c
entryToChange = 4
FileSeek(0, SizeOf(CheckInfo) * (entryToChange - 1))
WriteData(0, @checkInfo, SizeOf(CheckInfo))
CloseFile(0)
;- Read the data again
ReadFile(0, "c:\zooker.dat")
;- Read all data 0 thru 5 and note changes to 4th entry
Debug ""
Debug "Here is all of the data again after changing #4..."
Debug ""
For c = 1 To 5
FileSeek(0, SizeOf(Check) * (c - 1))
ReadData(0, @checkInfo, SizeOf(CheckInfo))
Debug "ID = " + Str(checkInfo\id)
Debug "Dayt = " + Str(checkInfo\Dayt)
Debug "Dec = " + checkInfo\Dec
Debug "Meno = " + checkInfo\Memo
Debug "Category = " + checkInfo\Category
Debug "Amount = " + StrD(checkInfo\Amount, 2)
Debug "Balance = " + StrD(checkInfo\Balance, 2)
Debug "========"
Next
CloseFile(0)
DeleteFile("c:\zooker.dat")
Structures
Posted: Sun Dec 07, 2008 10:35 pm
by Zooker
Heartfelt thanks to all you gentlemen who helped me! I've never had this much help this quick, and such great Quality. I'm almost speechless.
I have been playing around with Purebasic and I'm not really sure I can learn it. It's not quite as close to VB4-6 as I thought. I'm 78 years old, not as old as a Glacier but PRETTY old! I have a fair acguaintence with VB but I may have to attempt to lear PB. When you get old you get set in your ways. I'm sure I could learn (from your help) how to use structures but
that isn't the only stone in the Oyster. Thank You ALL!