Results 1 to 4 of 4

Thread: [RESOLVED] saving a multi-dimensional array to file?

  1. #1

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Resolved [RESOLVED] saving a multi-dimensional array to file?

    Is there an easy way to save then reload a multi-dimensional array to and from a file?
    In this case it's a 3D array.

    I just tried Put and Get, but they didn't seem to work.

  2. #2
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: saving a multi-dimensional array to file?

    Like this? or you are using a dynamic array?
    VB Code:
    1. private Arr(2, 2, 2) As String
    2.  
    3. Private Sub getArray()
    4.     Open "C:\myfile.txt" For Binary As #1
    5.         Get #1, , Arr
    6.     Close #1
    7. End Sub
    8.  
    9. Private Sub SaveArray()
    10.     Open "C:\myfile.txt" For Binary As #1
    11.         Put #1, , Arr
    12.     Close #1
    13. End Sub

  3. #3
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: saving a multi-dimensional array to file?

    If you're using a dynamic array:
    VB Code:
    1. private Arr() As String
    2.  
    3. Private Sub getArray()
    4.     Dim lngDim(2) As Long
    5.     Open "C:\myfile.txt" For Binary As #1
    6.         ' read the dimensions (12 bytes totaling of three Long values)
    7.         Get #1, , lngDim
    8.         ' clear array in case Arr is already filled with something
    9.         Erase Arr
    10.         ' initialize
    11.         ReDim Arr(lngDim(0), lngDim(1), lngDim(2))
    12.         ' fill the array
    13.         Get #1, , Arr
    14.     Close #1
    15. End Sub
    16.  
    17. Private Sub SaveArray()
    18.     Open "C:\myfile.txt" For Binary As #1
    19.         ' store dimensions as three long values (each one is 4 bytes)
    20.         Put #1, , CLng(UBound(Arr, 1))
    21.         Put #1, , CLng(UBound(Arr, 2))
    22.         Put #1, , CLng(UBound(Arr, 3))
    23.         ' store the actual array
    24.         Put #1, , Arr
    25.     Close #1
    26. End Sub

    Code is untested as I don't have VB on this machine; and I guess string arrays can be saved directly (I've never tried it myself, I like defining things more precisely; and I never use multidimension arrays anyway).

  4. #4

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: saving a multi-dimensional array to file?

    Working Great!

    Thx Merri

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