Results 1 to 3 of 3

Thread: Populate an array from a text file

  1. #1
    Guest

    Question

    Hi, I'm trying to fill up an array with single numbers from a file. the file is simple, it has rows and colums for the numbers and the first row is just the numbers of rows and colums of the array
    ie.

    text file:

    2 3

    12.3 45 55
    44.3 33.4 55

    The file would look something like that. I would like to know how to ReDIm the array with the first two numbers and then insert the table into the array, each number up to the space in each cell.

    I have no previous experience with manipulating files. Please help.

    Thanks in advance.

  2. #2
    Addicted Member jcouture100's Avatar
    Join Date
    Aug 1999
    Posts
    141
    Put this into your sub routine or function to get
    your data in an array. I'm assuming that your data file is located in "C:\Temp\Data.txt" in this example.

    Code:
        Dim arMyArray() As Single  'Dimension your array
        Dim sglValue As Single     'Defined as single because some values have decimal places
        Dim intRowCnt As Integer   'Counter for Rows in the array
        Dim intColCnt As Integer   'Counter for Columns in the array
        Dim ar1 As Integer         'Rows in Array
        Dim ar2 As Integer         'Columns in Array
        
        'Open data file for input.
        Open "C:\Temp\Data.txt" For Input As #1
        'Get array parameters from file
        Input #1, ar1, ar2
        'ReDim your array with the new values
        ReDim arMyArray(ar1, ar2)
        'Populate the array
        For intRowCnt = 0 To ar1 - 1
            For intColCnt = 0 To ar2 - 1
                Input #1, sglValue
                arMyArray(intRowCnt, intColCnt) = sglValue
            Next intColCnt
        Next intRowCnt
        'Close data file
        Close #1
    Hope this helps.
    JC

  3. #3
    Guest

    Wink Thanks

    Thank you.

    It worked perfectly!

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