Results 1 to 12 of 12

Thread: [RESOLVED] Fastest way to save/load variant array to/from file

  1. #1

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Resolved [RESOLVED] Fastest way to save/load variant array to/from file

    A have a variant array that could be one or two dimensions, could hold any data type, and I want to be able to save it to a file and read it back in from that file as quickly as possible.

    What's the absolute fastest way to do this? I don't much care what the actual file format ends up being. (Text, binary, whatever.)

  2. #2
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Fastest way to save/load variant array to/from file

    Binary is the fastest way. In that way it states the same as it was in memory. So you would need to use arrays then write the whole array at once.

  3. #3

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Fastest way to save/load variant array to/from file

    That sounds ideal. Could you start me off with a code snippet? Doesn't have to be anything fancy.

  4. #4
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Fastest way to save/load variant array to/from file

    What kind of structure are you attempting to write to file? Post an example to work with.

  5. #5
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington DC
    Posts
    314

    Re: Fastest way to save/load variant array to/from file

    In QBasic, I would recommend BSAVE/BLOAD, but I see those commands are not in VB.

    Mac

  6. #6

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Fastest way to save/load variant array to/from file

    Quote Originally Posted by randem
    What kind of structure are you attempting to write to file? Post an example to work with.
    The structure is variable. Here's some examples:
    Code:
    Sub Main()
        Dim varArray As Variant
        Dim lngArray() As Long
        Dim strFile As String
        Dim i As Long
    
        strFile = App.Path & "\Array"
    
        ' Two-dimensional
        ReDim varArray(1,1)
        varArray(0,0) = 1
        varArray(0,1) = "Hello"
        varArray(1,0) = 2
        varArray(1,1) = ", World!"
        SaveArrayToFile varArray, strFile
        Erase varArray
        LoadArrayFromFile varArray, strFile
        Debug.Print varArray(0,1) & varArray(1,1)
        Erase varArray
        
        ' Single-dimension Date
        ReDim varArray(10)
        For i = 0 To 10
            varArray(i) = DateAdd("d", i, Now())
        Next
        SaveArrayToFile varArray, strFile
        Erase varArray
        LoadArrayFromFile varArray, strFile
        Debug.Print "3 days from now is " & varArray(3)
        Erase varArray
        
        ' Typed single-dimension Long
        ReDim lngArray(10)
        For i = 0 To 10
            lngArray(i) = 7 * i
        Next
        SaveArrayToFile lngArray, strFile
        Erase lngArray
        LoadArrayFromFile lngArray, strFile
        Debug.Print "7 * 9 = " & lngArray(9)
        Erase lngArray
    
        ' Single-dimension string
        ReDim varArray(1)
        varArray(0) = "Hello,"
        varArray(1) = " World!"
        SaveArrayToFile varArray, strFile
        Erase varArray
        LoadArrayFromFile varArray, strFile
        Debug.Print varArray(0) & varArray(1)
        Erase varArray
    End Sub
    
    Public Sub SaveArrayToFile(pvarArray As Variant, pstrFile As String)
        Dim strFile As String
        Dim strHeader As String 
    
        strFile = pstrFile & ".dat"
        strHeader = pstrFile & ".hdr"
    
        ???
    End Sub
    
    Public Sub LoadArrayFromFile(pvarArray As Variant, pstrFile As String)
        Dim strFile As String
        Dim strHeader As String 
    
        strFile = pstrFile & ".dat"
        strHeader = pstrFile & ".hdr"
    
        ???
    End Sub
    No need to worry about validating that the passed array is in fact an array; I can handle all the validation. Also, if saving the structure information to the same file is just as fast, no need to split it off to a second file.

    So the arrays will hold any native data type (no UDTs or objects) and be either 1- or 2-dimensional. The passed arrays may be originally declared either as a typed array or simply as a Variant.
    Last edited by Ellis Dee; Jul 24th, 2007 at 08:15 PM.

  7. #7

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Fastest way to save/load variant array to/from file

    This function may be of help:
    Code:
    ' Returns 0 for unintialized array, -1 for non-array
    Public Function ArrayDimensions(pvarArray As Variant) As Long
        Dim lngTemp As Long
        Dim i As Long
        
        On Error Resume Next
        Do
            i = i + 1
            lngTemp = UBound(pvarArray, i)
            Select Case Err.Number
                Case 13: ArrayDimensions = -1
                Case 9: ArrayDimensions = i - 1
            End Select
        Loop Until Err.Number
    End Function

  8. #8

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Fastest way to save/load variant array to/from file

    I'm hurtin' for a solution on this one. Does nobody have any ideas?

    Even if it's not all-purpose, I would appreciate any kind of approach. How about we start off with just a single-dimension variant array holding Longs.

    Anyone?

  9. #9
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Fastest way to save/load variant array to/from file

    Could you repost your code in post #6 or replace those vbcode tags with just the code tags. Those numbers are annoying to get rid of in a copy/paste. I will implement something.
    Last edited by randem; Jul 24th, 2007 at 08:11 PM.

  10. #10

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Fastest way to save/load variant array to/from file

    No problem, sorry about that.

  11. #11
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Fastest way to save/load variant array to/from file

    Here ya go... Enjoy.
    Attached Files Attached Files

  12. #12

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Fastest way to save/load variant array to/from file

    Aw yeah, now that is what I'm talking about.

    Thanks much, randem. It's as simple as simple gets. You rock.

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