Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Reading and Parsing a text file into VB

Status
Not open for further replies.

filos123

Computer
Oct 18, 2001
1
I used to work with Excel Vb files where I would open the text file in Excel and manipulate the information from Visual Basics. The files have gotten much bigger and Excel can only handle 65000 lines. I need to open the file directly into VB and assign each parsed line to a multi dimensional array.

Second question - if you have an multidimentional array - how do I sort on that?

I would appreciate some code samples. thanks
 
Replies continue below

Recommended for you

How is your text file formatted? Is each line comma delimeted? How many dimensions is your array going to contain? What type of data are you reading in and how do you want to sort the values?

Unless you know how many rows of data you have, it would be better to dynamically change the size of the array. Unfortunatly, you can not do that on a multi-dimensional array. Here is a sample, where the text file contains this:

text.txt
1,2,3
4,5,6
7,8,9

Code:
Option Explicit
Option Base 1

Sub ImportData()
    Dim sFile As String, sMsg As String
    Dim Val1() As Integer, Val2() As Integer, Val3() As Integer
    Dim iIdx As Long
    
    sFile = "C:\test.txt"
    
    iIdx = 1
    Open sFile For Input As #1
        Do While Not EOF(1)
            ReDim Preserve Val1(iIdx)
            ReDim Preserve Val2(iIdx)
            ReDim Preserve Val3(iIdx)
            Input #1, Val1(iIdx), Val2(iIdx), Val3(iIdx)
            iIdx = iIdx + 1
        Loop
    Close
    
    sMsg = "Values:" & vbCrLf
    For iIdx = LBound(Val1) To UBound(Val1)
        sMsg = sMsg & "Idx: " & iIdx & " - (" & _
               Val1(iIdx) & ", " & Val2(iIdx) & _
               ", " & Val3(iIdx) & ")" & vbCrLf
    Next iIdx
    
    MsgBox sMsg
    
    ReDim Val1(1): ReDim Val1(2): ReDim Val1(3)

End Sub
NOTE: If you are going to read in your large file, take the MsgBox portion out. I don't think it will like you too much if you try to stuff that huge string in there.

Hope this helps to get you started. I have some sorting routines, but I need a little more info first. DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
 
Try this,

dim ff as integer
dim raw as string
dim Fname as string 'the path to the file
dim lines() as string 'used to hold the lines of the text file

ff = FreeFile

Open Fname For Binary As #ff
Raw = String$(LOF(ff), 32)
Get #ff, 1, Raw
Close #ff
lines() = Split(Raw, vbnewline) 'this assumes that the data is stored in individual lines.

dim multiArray(1 to upper(lines),1 to 5, 1 to 7) as variant 'or whatever datatype

Instead of a multi dimensional array I would suggest using a collection of objects.

To sort the array, use one of the classical sorting algorithms, bubble, insertion, quick sort....

If you would give more details about how the data is formated in the text file I could be of more assistance.


Troy Williams
fenris@hotmail.com
 
i want to make a vb program tts usin a wav file and dsound files
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor