Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A faster way to load a text file into an array

Folders and Files

A faster way to load a text file into an array

by  Alt255  Posted    (Edited  )
One of the most frequent questions in the VB forum is "How do read a text file into an array?". The standard answer usually consists of this:

1) Open the file for input.
2) Read each line of the file and store the data in an array.
3) Quit reading the file when it reaches the end (EOF).

Here is an example. This uses [tt]Redim Preserve[/tt] to add a new array element every time a new line is read.
[tt]Open "MyFile.txt" For Input As #1
ReDim Txt$(0)
Do While Not EOF(1)
ReDim Preserve Txt$(UBound(Txt$) + 1)
Input #1, Txt$(UBound(Txt$))
Loop
Close #1[/tt]


The problem with this approach is that it uses a very inefficient method to read the file. Every line of the file requires a distinct file operation. The following method is many times faster. It can read a large text file and store it in an array in approximately the same amount of time the previous method requires to read only one line of the file. The trick is to access the disk only one time, rather than thousands of times. Once the file is in memory, parsing and manipulating the data is easy and extremely fast.

Place a Drive listbox, a Dir listbox and a File listbox on a form and try it out. Optionally, you might want to add a Listbox so you can verify that the method actually works.

[tt]
Private Sub Drive1_Change()
Dir1 = Drive1
End Sub

Private Sub Dir1_Change()
File1 = Dir1
End Sub

Private Sub File1_Click()
If Right$(Dir1.Path, 1) = "\" Then
Fname$ = Dir1.Path & File1.FileName
Else
Fname$ = Dir1.Path & "\" & File1.FileName
End If

ff = FreeFile
Open Fname$ For Binary As #ff
Raw$ = String$(LOF(ff), 0)
'Read the entire file in one operation and close it
Get #ff, 1, Raw$
Close #ff

ReDim Txt$(0)
Do
Cloc = InStr(Raw$, vbCrLf)
If Cloc > 0 Then 'Parse the data
ReDim Preserve Txt$(0 To UBound(Txt$) + 1)
Tmp$ = Left$(Raw$, Cloc - 1)
Txt$(UBound(Txt$)) = Tmp$
Else
Exit Do 'All data has been parsed
End If
Raw$ = Right$(Raw$, Len(Raw$) - (Len(Tmp$) + 2))
Loop

'Let's load the array in a List box
'to make sure everything worked properly.
List1.Clear
For Rep = 1 To UBound(Txt$)
List1.AddItem Txt$(Rep)
Next

End Sub
[/tt]

If you are using VB6, there is an even easier and faster way to place the file in an array:[tt]

ff = FreeFile
Open Fname$ For Binary As #ff
Raw$ = String$(LOF(ff), 32)
Get #ff, 1, Raw$
Close #ff
Txt$ = Split(Raw$, vbCrLf)
[/tt]

This places the entire contents of the file into the Txt$ array. The array is "zero based" so Txt$(0) contains the first line of the file and Txt$(Ubound(Txt$)) contains the last line of the file.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top