Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
TitleLoad an array from a file in three ways
Keywordsarray, load array, file, Input
CategoriesSoftware Engineering, Files and Directories
 
This program first uses the Input # statement to load the data.
 
' Load using the Input # statement.
Private Sub cmdInput_Click()
Dim fnum As Integer
Dim num_lines As Long
Dim the_array() As Integer
Dim i As Long
Dim start_time As Single
Dim stop_time As Single

    Screen.MousePointer = vbHourglass
    DoEvents

    num_lines = CLng(txtLines.Text)
    ReDim the_array(1 To num_lines, 1 To 3)

    fnum = FreeFile

    start_time = Timer
    Open txtFile.Text For Input As fnum
    i = 1
    Do While Not EOF(fnum)
        Input #fnum, the_array(i, 1), the_array(i, 2), _
            the_array(i, 3)
        i = i + 1
    Loop
    Close #fnum
    stop_time = Timer

    lblInput.Caption = Format$(stop_time - start_time, _
        "0.00") & " sec"
    Screen.MousePointer = vbDefault
End Sub
 

Next the program usees Input to read the whole file at once. It then uses InStr to find the delimiters between lines and between numbers on each line. It copies the results into the array.

 
' Load using the Input function.
Private Sub cmdInputFunction_Click()
Dim fnum As Integer
Dim num_lines As Long
Dim all_lines As String
Dim the_array() As Integer
Dim i As Long
Dim start_time As Single
Dim stop_time As Single
Dim next_line As String
Dim next_number As String
Dim pos1 As Long
Dim pos2 As Long
Dim pos3 As Long
Dim pos4 As Long

    Screen.MousePointer = vbHourglass
    DoEvents

    num_lines = CLng(txtLines.Text)
    ReDim the_array(1 To num_lines, 1 To 3)

    fnum = FreeFile

    start_time = Timer

    ' Grab the file contents.
    Open txtFile.Text For Input As fnum
    all_lines = Input(LOF(fnum), #fnum)
    Close #fnum

    ' Parse out the numbers.
    pos1 = 1
    For i = 1 To num_lines
        ' Find the next line.
        pos2 = InStr(pos1, all_lines, vbCrLf)
        next_line = Mid$(all_lines, pos1, pos2 - pos1)

        ' Get the numbers.
        pos3 = 1
        pos4 = InStr(pos3, next_line, ",")
        next_number = Mid$(next_line, pos3, pos4 - pos3)
        the_array(i, 1) = CInt(next_number)
        pos3 = pos4 + 1

        pos4 = InStr(pos3, next_line, ",")
        next_number = Mid$(next_line, pos3, pos4 - pos3)
        the_array(i, 2) = CInt(next_number)

        next_number = Mid$(next_line, pos4 + 1)
        the_array(i, 3) = CInt(next_number)

        pos1 = pos2 + 2
    Next i
    stop_time = Timer

    lblInputFunction.Caption = Format$(stop_time - _
        start_time, "0.00") & " sec"
    Screen.MousePointer = vbDefault
End Sub
 
Finally, the program loads the whole file at once and parse out the numbers using Split.
 
Private Sub cmdInputSplit_Click()
Dim fnum As Integer
Dim num_lines As Long
Dim all_lines As String
Dim lines As Variant
Dim numbers As Variant
Dim the_array() As Integer
Dim i As Long
Dim j As Long
Dim start_time As Single
Dim stop_time As Single

    Screen.MousePointer = vbHourglass
    DoEvents

    num_lines = CLng(txtLines.Text)
    ReDim the_array(1 To num_lines, 1 To 3)

    fnum = FreeFile

    start_time = Timer

    ' Grab the file contents.
    Open txtFile.Text For Input As fnum
    all_lines = Input(LOF(fnum), #fnum)
    Close #fnum

    ' Parse out the numbers.
    lines = Split(all_lines, vbCrLf)
    For i = 0 To num_lines - 1
        ' Get the numbers.
        numbers = Split(lines(i), ",")
        For j = 0 To 2
            the_array(i + 1, j + 1) = numbers(j)
        Next j
    Next i
    stop_time = Timer

    lblInputSplit.Caption = Format$(stop_time - start_time, _
        "0.00") & " sec"
    Screen.MousePointer = vbDefault
End Sub
 

The first method is fastest, the second is slower, and the third method is slowest (although probably easiest).

NOTE: Before you run this program, use the included MakeNums program to make a data file to load. Be sure you enter the same number of items in both programs.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated