reading files into a 2D array without knowing how many records in file

zucko1389
zucko1389 used Ask the Experts™
on
I need to know how to set up a counter so when i read a file i can put the information into a 2D array
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Explain file format

Author

Commented:
file should read like

"seg","gls","miles","cost"
"1","21","22","10"

with the same number of columns, but with indeterminate amount of rows
I have done quite a bit of textfile processing with records that don't look too different from the ones you are working with.  The approach I use will work very well for you but it will not involve using arrays.

Try using a collection of objects instead.  The most simple way to do this would be to create a class with only a data member which is a string.  Create public functions to set and get the value of that string (the vb class builder can make this very easy indeed).  Now when you read your text file, with each record create a new instance of the class you have defined and stick the data into the string property.  Add the object to your collection, and you're golden.

When you get to the end of the file your objCollection.Count will be equal to the number of records you have processed, and each object in the collection will have one line of text.

A more advanced version of this method would feature a record object that can parse the data for you, so that less of that manual work is required when you start to use the data later in your application.
   Dim iFN As Integer
    Dim strArray() As String
    Dim lLines As Long
    Dim strCol0 As String
    Dim strCol1 As String
    Dim strCol2 As String
    Dim strCol3 As String
   
    iFN = FreeFile()
    Open "c:\temp\x.txt" For Input As #iFN
    While Not EOF(iFN)
        Input #iFN, strCol0, strCol1, strCol2, strCol3
        lLines = lLines + 1
        ReDim Preserve strArray(3, lLines - 1)
        strArray(0, lLines - 1) = strCol0
        strArray(1, lLines - 1) = strCol1
        strArray(2, lLines - 1) = strCol2
        strArray(3, lLines - 1) = strCol3
    Wend
    Close #iFN

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial