Saving Structures to disk and reading structures from Disk

Just starting out? Need help? Post your questions and find answers here.
Zooker
User
User
Posts: 22
Joined: Sun Apr 27, 2003 1:46 am
Location: So.Illinois USA

Saving Structures to disk and reading structures from Disk

Post 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? :?
It took 73 years for me to get this Stupid!
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post 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)
 
Last edited by Sparkie on Sun Dec 07, 2008 2:39 pm, edited 1 time in total.
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post 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
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post 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) 
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

does this mean and individual file for each structure?
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Post 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 !
pb 5.11
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post 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") 
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Zooker
User
User
Posts: 22
Joined: Sun Apr 27, 2003 1:46 am
Location: So.Illinois USA

Structures

Post 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!
It took 73 years for me to get this Stupid!
Post Reply