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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB and tab-delimited files 3

Status
Not open for further replies.

tbohon

Programmer
Apr 20, 2000
293
US
I'm trying to read a tab-delimited file into a VB array so that it can be manipulated in memory. The file consists of a half dozen or so fields, each separated by a tab character and with each line ending with a cr/lf pair.

Short of reading in character-by-character (too time consuming with the size of the file) does anyone have a trick up his/her sleeve to do this?

Thanks in advance!

Tom
 
Try this code. Your entries will in an array.
Code:
Option Explicit

Private Sub Form_Load()
    Dim intFile As Integer
    Dim strBuffer As String
    Dim varArray As Variant
    
    intFile = FreeFile
    
    Open "C:\temp\test.txt" For Input As #intFile
    
    Do Until EOF(intFile)
        Line Input #intFile, strBuffer
        varArray = Split(strBuffer, vbTab)
    Loop
    
    Close #intFile
End Sub
Snaggs
tribesaddict@swbell.net
A procrastinator's work is never done.
 
Thanks for the info - is Split something in VB6 perhaps? I've tried it in both VB4 and VB5 - 'undefined procedure' type of error.

Tom
 
Yep, Split is new for VB 6.0. Snaggs
tribesaddict@swbell.net
A procrastinator's work is never done.
 
Hi Snaggs,
Will the splitted function be overwritten in the varArray ? or will it be appendend as a new row in the array ?

Can you please clarify ?

Thanks in advance.

RR
 
Hi Snaggs,

I am rephrasing the question. :)

Will the split function overwrite the varArray ? or will it be appendend as a new row in the array ?

Can you please clarify ?

Thanks in advance.

RR
 
When I tested your code, it actually overwrites the previous values in varArray. Do you know a way to avoid this and instead a way to create a true multidimensional array ?

Thanks in advance.

RR
 
If it were Me, I'd use a collection, but here's the code written using an array.

Code:
Option Explicit

Private Sub Form_Load()
    Dim intFile As Integer, intRow As Integer, intCol As Integer
    Dim intRows As Integer
    Dim strBuffer As String
    Dim varArray As Variant
    Dim varNames As Variant
    
    ReDim varNames(0) As Variant
    
    intFile = FreeFile
    
    Open "C:\temp\test.txt" For Input As #intFile
    
    intRows = 0
    
    Do Until EOF(intFile)
        'Read the line in from the file.
        Line Input #intFile, strBuffer
        
        'Pick off the columns and put then in a seperate array.
        varArray = Split(strBuffer, vbTab)
        
        'Set the size of the array to hold the data.
        'Be sure that the intCol value doesn't change from row to row.
        ReDim Preserve varNames(intRows)
        
        varNames(intRows) = varArray
        intRows = intRows + 1
    Loop
    
    Close #intFile
    
    For intRow = 0 To intRows - 1
        For intCol = 0 To UBound(varNames(intRow))
            Debug.Print varNames(intRow)(intCol);
        Next intCol
        Debug.Print
    Next intRow
End Sub

Here's the file I used to test the code with:

Code:
G.I.	Joe
Mr.	Potatohead
Alan	Greenspan
Max	Headroom
Snaggs
tribesaddict@swbell.net
To define recursion, we must first define recursion.
 
Hey Snaggs,

Thank you very much for hitting the Bull's Eye for me.

Beautifully structured code!!! Thanks again for the Neat help.

RR.
:)
 
There have recently been several questions on the subject and different working solutions are proposed in this thread or elsewhere: faq222-482.

The closest approach to loading a CSV file into an array was proposed by Snaggs in this thread. It uses an Array of Arrays, which involves the use of Variants. While it does the job, the program code is hard to read (and subsequently to maintain). Another critique is that it doesn't use the standard way of indexing elements of the Array. varNames(intRow)(intCol) is not an Array notation: varNames(intRow,intCol). Suppose that later on in the program you want to Multiply this Array with another one: the indexing quickly becomes unmanageable.

I do not offer a perfect solution, but at least it allows for standard use of Array notation. I do admit it is a memory greedy one (would it really be that much greedy ?), but the code is compact and (hopefully) readably.

I didn't do a benchmark against the other techniques, but my best bet is that it will stand up if files are not too small.

Const c_strFileName As String = "D:\FsoCsvToArray\TestFile.txt"

Public Sub Main()

Dim astrTempNames() As String
Dim objFso As FileSystemObject
Dim objTextStream As TextStream


'Split entire file into a one-dimensional array

Set objFso = New FileSystemObject
Set objTextStream = objFso_OpenTextFile(c_strFileName, ForReading)
astrTempNames = Split(objTextStream.ReadAll, vbCrLf)
Set objTextStream = Nothing
Set objFso = Nothing


'We use the First row to determine the number of elements in a Row
' All rows are supposed to have the same number of elements

Dim astrTempRow() As String
astrTempRow = Split(astrTempNames(0), vbTab)

Dim astrNames() As String
ReDim astrNames(UBound(astrTempNames), UBound(astrTempRow))


'Because we splitted already the first row, we can as well
' insert it already into the Definitive Array

InsertRowIntoArray astrTempRow, astrNames, 0


'Insert all other rows

Dim lngRowIndex As Long
For lngRowIndex = 1 To UBound(astrTempNames)
astrTempRow = Split(astrTempNames(lngRowIndex), vbTab)
InsertRowIntoArray astrTempRow, astrNames, lngRowIndex
Next lngRowIndex


'Clean Up

Erase astrTempRow
Erase astrTempNames

End Sub

Private Sub InsertRowIntoArray(astrRow() As String, astrArray() As String, lngRowNumber As Long)

Dim lngRowIndex As Long
For lngRowIndex = 0 To UBound(astrRow)
astrArray(lngRowNumber, lngRowIndex) = astrRow(lngRowIndex)
Next lngRowIndex

End Sub


Any critiques are wellcome!

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
rvBasic, Simply Superb!! Amazed at your solution!!!!


Please see a posting called F1 Please - Interesting Design Problem, and please see if you can offer me a solution for that posting.


Thank you very much again.

RR
:)




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top