|
-
Jul 13th, 2001, 08:19 AM
#1
Thread Starter
Fanatic Member
writing to file
how can i write an entire structure to a file without putting each individual member in seperately.
here is the part of my code that deals with this:
Code:
Private Type Deviations
Average As Double
Deviation As Double
End Type
Private Type Statistics
HydrogenCounts As Long
OxygenCounts As Long
BackgroundCounts As Long
HydrogenCorrected As Double
OxygenCorrected As Double
Ratio As Double
End Type
Private Type FileInfo
Filename As String
Path As String
Import As String
Export As String
Workbook As String
PictureFile As String
Filter As String
Title As String
Prefix As String
End Type
Private Type BinNumber
Bin(1 To 1024) As Long
End Type
Private Type tofFiles
Info As FileInfo
IsFileCalculated As Boolean
SumData(1 To 1024) As Long
TotalRecords As Integer
RawData() As BinNumber
RawStats() As Statistics
SumStats As Statistics
Deviation As Deviations
End Type
Dim tofFile() As tofFiles
Private Sub Form_Unload(Cancel As Integer)
Open App.Path & "\Data.tof" For Binary As #1
Put #1, , tofFile()
Close #1
Unload Form2
End Sub
Private Sub Form_Load()
Open App.Path & "\Data.tof" For Binary As #1
Get #1, , tofFile()
Close #1
End Sub
As you can see i have quite a few arrays that aren't totally declared yet, cuz i need to resize them during run-time. I thought i remembered using this same method before, but i don't know if i had undimensioned arrays, is that the problem? vb isn't smart enough to save arrays that don't have fixed lengths?
thanx
-
Jul 13th, 2001, 08:37 AM
#2
Member
To cycle through each item in an array if you don't know the array's size, use UBound() and LBound(). For example, if you had an array called MyArray....
VB Code:
Dim i as Integer
For i = LBound(MyArray) To UBound(MyArray)
Debug.Print("MyArray(" & i & ") = " & MyArray(i)
Next i
-
Jul 13th, 2001, 08:48 AM
#3
Thread Starter
Fanatic Member
exactly
thats what i want to avoid doing
i already know the LBound is 1 so thats not a problem, but i don't want to use UBound becuase then when I'm reading it from the file I'll have to have stored the UBound value somewhere in the file so i can retrieve the data correctly.
Also, did you see how many members i have? i know it won't be too bad with multi loops, but i just wondered if there was an easier way so in the future if i add any other stuff to the structures i don't have to edit the get and put stuff
-
Jul 13th, 2001, 08:51 AM
#4
Member
Do you know if an Index out of Bounds error is trappable (using an On Error Goto x)? If so, just use an infinite While loop (While True), and when you reach the end of the array, an error will be thrown and you'll exit the loop. For example:
VB Code:
On Error Goto EndOfLoop
Dim i as Integer
i = 1 ' According to you, the LBound
While True ' Don't do this too often :)
Debug.Print MyArray(i)
i = i + 1 ' Man, I miss the ++ operator from C++ and Java!
Wend
EndOfLoop:
Debug.Print "Done: there are " & i & " elements."
-
Jul 13th, 2001, 09:38 AM
#5
Thread Starter
Fanatic Member
awesome
this is cool, i figured it out
it does write it to the file correctly, but you have to dimension all the arrays to the exact number of elements before you can read it back in
Code:
Private Sub Form_Load()
Open App.Path & "\Data.tof" For Binary As #1
Get #1, , Total
If Total <> 0 Then
ReDim tofFile(1 To Total) As tofFiles
For i = 1 To Total
Get #1, , tofFile(i).TotalRecords
ReDim tofFile(i).RawData(1 To tofFile(i).TotalRecords) As BinNumber
ReDim tofFile(i).RawStats(1 To tofFile(i).TotalRecords) As Statistics
Next i
Get #1, , tofFile()
End If
Close #1
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Total <> 0 Then
Open App.Path & "\Data.tof" For Binary As #1
Put #1, , Total
For i = 1 To Total
Put #1, , tofFile(i).TotalRecords
Next i
Put #1, , tofFile()
Close #1
End If
Unload Form2
End Sub
-
Jul 13th, 2001, 09:38 AM
#6
Why not just save the UBound of the array when saving, then redefine that array when loading? e.g:
To save:
VB Code:
Open "C:\Windows\Desktop\MyFile" For Binary As #1
Put #1, , UBound(struct)
Put #1, 10, struct()
Close #1
To load:
VB Code:
Dim struct As tofFiles
Open "C:\Windows\Desktop\MyFile" For Binary As #1
Get #1, , amount
ReDim struct(amount)
Get #1, 10, struct()
Close #1
-
Jul 13th, 2001, 06:48 PM
#7
Thread Starter
Fanatic Member
because...
there are other arrays inside the structure that are of arbitrary length that have to be resized.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|