Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Visual Basic (Microsoft) Versions 5/6 FAQ

Folders and Files

A faster way to load a text file into an array by Alt255
Posted: 11 Feb 01

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 Redim Preserve to add a new array element every time a new line is read.
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$))
Close #1

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.

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
    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)
    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$
        Exit Do  'All data has been parsed
    End If
    Raw$ = Right$(Raw$, Len(Raw$) - (Len(Tmp$) + 2))

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

End Sub

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

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

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.

Back to Visual Basic (Microsoft) Versions 5/6 FAQ Index
Back to Visual Basic (Microsoft) Versions 5/6 Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close