Results 1 to 7 of 7

Thread: writing to file

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    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

  2. #2
    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:
    1. Dim i as Integer
    2. For i = LBound(MyArray) To UBound(MyArray)
    3.     Debug.Print("MyArray(" & i & ") = " & MyArray(i)
    4. Next i

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    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

  4. #4
    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:
    1. On Error Goto EndOfLoop
    2.  
    3. Dim i as Integer
    4. i = 1 ' According to you, the LBound
    5. While True ' Don't do this too often :)
    6.     Debug.Print MyArray(i)
    7.     i = i + 1 ' Man, I miss the ++ operator from C++ and Java!
    8. Wend
    9.  
    10. EndOfLoop:
    11.    Debug.Print "Done: there are " & i & " elements."

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    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

  6. #6
    Megatron
    Guest
    Why not just save the UBound of the array when saving, then redefine that array when loading? e.g:

    To save:
    VB Code:
    1. Open "C:\Windows\Desktop\MyFile" For Binary As #1
    2. Put #1, , UBound(struct)
    3. Put #1, 10, struct()
    4. Close #1

    To load:
    VB Code:
    1. Dim struct As tofFiles
    2.  
    3. Open "C:\Windows\Desktop\MyFile" For Binary As #1
    4. Get #1, , amount
    5. ReDim struct(amount)
    6. Get #1, 10, struct()
    7. Close #1

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    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
  •  



Click Here to Expand Forum to Full Width