Log inUsernamePassword
Log me on automatically each visit    
Register
Register
Log in to check your private messages
Log in to check your private messages
Visual Basic Forum for Visual Basic Programmers VB Forum Index » Registry & File Input / Output

Post new topic   Reply to topic
Multiple Multi-demensional Array Saving and Loading
View previous topic :: View next topic  
Author Message
Tony Epic
Newbie


Joined: 16 May 2006
Posts: 4

PostPosted: May 16th, 2006 10:45 AM    Post subject: Multiple Multi-demensional Array Saving and Loading Reply with quote

I think I need actual working code samples for this.
I want to save two arrays PX(X, Y) and PY(X, Y) to
a file and Load them again. Its for a Drawing program.
Any help would be greatly appreciated. Thanks. Biggrin

-Tony Razz
Back to top
View user's profile Send private message Send e-mail
dougthomas
Moderator


Joined: 27 Jul 2005
Posts: 271
Location: Essex, UK

PostPosted: May 20th, 2006 03:02 AM    Post subject: Reply with quote

Hi,

Try this:
Code:

Option Explicit
'
' Define PX and PY as Dynamic Arrays
'
Private PX() As Single
Private PY() As Single
Private strFile As String

Private Function GetRandom() As Integer
GetRandom = Int(50 * Rnd + 50)
End Function

Private Sub FillArray(sngArray() As Single)
'
' For testing Purposes
' Fill the array with random numbers
'
Dim intI As Integer
Dim intJ As Integer
For intJ = LBound(sngArray, 1) To UBound(sngArray, 1)
    For intI = LBound(sngArray, 2) To UBound(sngArray, 2)
        sngArray(intJ, intI) = 50 * Rnd + 50
    Next intI
Next intJ
End Sub

Private Sub SaveArray(sngArray() As Single, intFile As Integer)
'
' Save each element of the array
'
Dim intI As Integer
Dim intJ As Integer
For intJ = LBound(sngArray, 1) To UBound(sngArray, 1)
    For intI = LBound(sngArray, 2) To UBound(sngArray, 2)
        '
        ' The Format() statement is optional
        ' makes the file look 'pretty'
        '
        Print #1, Format(sngArray(intJ, intI), "####.## ");
    Next intI
    Print #1,
Next intJ
End Sub

Private Sub LoadArray(sngArray() As Single, intFile As Integer)
'
' Read each element in the same order they were saved
'
Dim intI As Integer
Dim intJ As Integer
For intJ = LBound(sngArray, 1) To UBound(sngArray, 1)
    For intI = LBound(sngArray, 2) To UBound(sngArray, 2)
        Input #intFile, sngArray(intJ, intI)
    Next intI
Next intJ
End Sub

Private Sub cmdLoad_Click()
'
' Load the arrays from a file
'
Dim intFile As Integer
Dim intLx As Integer
Dim intLy As Integer
Dim intUx As Integer
Dim intUy As Integer
Dim intI As Integer
Dim intJ As Integer
intFile = FreeFile
Open strFile For Input As intFile
'
' The first 2 records are the dimensions of the arrays
'
Input #intFile, intLx, intLy, intUx, intUy
ReDim PX(intLx To intUx, intLy To intUy)
Input #intFile, intLx, intLy, intUx, intUy
ReDim PY(intLx To intUx, intLy To intUy)
'
' Load the arrays
'
Call LoadArray(PX, intFile)
Call LoadArray(PY, intFile)
Cloas intFile
MsgBox "Arrays Loaded"
End Sub

Private Sub cmdSave_Click()
'
' Save contents of arrays to a file
'
Dim intFile As Integer
intFile = FreeFile
Open strFile For Output As intFile
'
' Start off by saving the Dimensions of each array
' as the first 2 records so we know how to dimension the
' arrays when we want to load them
'
Print #intFile, LBound(PX, 1), LBound(PX, 2), UBound(PX, 1), UBound(PX, 2)
Print #intFile, LBound(PY, 1), LBound(PY, 2), UBound(PY, 1), UBound(PY, 2)
'
' Now save each array
'
Call SaveArray(PX, intFile)
Call SaveArray(PY, intFile)
Close intFile
MsgBox "Data Saved"
End Sub

Private Sub Form_Load()
Dim intRndX As Integer
Dim intRndY As Integer
Dim intI As Integer
'
' Set up some data in the Arrays
' for testing purposes
'
Randomize
'
' Create two random numbers to dimension
' each array between 50 and 100 elements
'
intRndX = GetRandom
intRndY = GetRandom
ReDim PX(intRndX, intRndY)
intRndX = GetRandom
intRndY = GetRandom
ReDim PY(intRndX, intRndY)
'
' Now generate some values for each array
'
Call FillArray(PX)
Call FillArray(PY)
strFile = "C:\TestArray.txt"
End Sub

Obviously, if the arrays static (ie.always have the same dimensions) you don't need to save the dimensions to the file and the ReDims will have to be removed.
Regards
Doug
_________________
If you can see the light at the end of the tunnel, it probably means there's a Train coming.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Visual Basic Forum for Visual Basic Programmers VB Forum Index » Registry & File Input / Output All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Visual Basic Forum runs phpBB | Forum Template © iOptional
VB Resources | SSL | Visual Basic