Results 1 to 3 of 3

Thread: Importing Tab Delimited Files

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2000
    Posts
    82
    I have to write a routine to import a Tab delimited file into an array. Nothing I do seems to work easily without the following work-around.
    Code:
                Line Input #iInHandle, sLine
                iCtr2 = 1
                ' "sTemp" hold data as we read it in
                sTemp = ""
                ' parse the line to the array elements - look for "iDelimitChar" which
                ' is the delimiter between fields
                For iCtr = 1 To Len(sLine)
                    ' look for delimiter
                    If Mid(sLine, iCtr, 1) <> Chr(iDelimitChar) Then
                        ' it wasn't a delimiter char, so add it to "sTemp"
                        sTemp = sTemp & Mid(sLine, iCtr, 1)
                    Else
                        ' it was a delimiter, so we are thru with this element - load the
                        ' array element with the value of "sTemp"
                        sArray(iCtr2) = sTemp
                        ' re-initialize "sTemp" and increment the element counter ("iCtr2")
                        sTemp = ""
                        iCtr2 = iCtr2 + 1
                    End If
                Next iCtr
                ' Once we fall out of the loop, it's because we have reached the end
                ' of the line. Since there is no trailing delimiter, then everything in
                ' "sTemp" will go into the last array used element
                sArray(iCtr2) = sTemp
    Doing it this way works, but is s-l-o-w. Can anyone speed up this code? BTW, the client cannot change to a comma delimited format, which would be real easy for us.
    Thanks.

  2. #2
    Addicted Member
    Join Date
    Sep 2000
    Posts
    138
    Using the Split() function would perhaps be better for your purpose. Like this:

    Code:
    -------------------------------------
    Dim strFileName As String
    Dim intFileNum As Integer
    Dim strContent As String
    Dim strArray() As String

    intFileNum = FreeFile
    strFileName = "PathAndFileName"
    Open strFileName For Binary As #intFileNum
    strContent = String(LOF(intFileNum), 0)
    Get #intFileNum, , strContent
    Close intFileNum
    strArray = Split(strContent, vbTab)
    ------------------------------------

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2000
    Posts
    82
    Thanks...slick solution.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width