Results 1 to 11 of 11

Thread: [RESOLVED] How to read some specific columns from a text file ?

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member Daniel Duta's Avatar
    Join Date
    Feb 2011
    Location
    Bucharest, Romania
    Posts
    404

    Resolved [RESOLVED] How to read some specific columns from a text file ?

    Hello VB friends,

    I have the following usual lines of code written to open a database in the text format. The database has 5 columns but for me are useful only two (col1 and col 3, which contain cities). My goal is to join these two columns in a single string (bigCities). I used the Input method because I have considered it is fast enough for my needs and a two dimensional array (myArr).

    The issue is that I don't now how to avoid to load other 3 unuseful columns as long as I need only 2...I wonder if there are other more suitable aproach.

    Code:
    Dim fnum As Integer, i As Long
    Dim myArr() As String, bigArray As String
    Dim bigCities As String
    ReDim myArr(1 To 5, 10000)
    
    fnum = FreeFile
    Open "myFile" For Input As #fnum
    i = 0
    Do While Not EOF(fnum)
    Input #fnum, myArr(1, i), myArr(2, i), myArr(3, i), myArr(4, i), myArr(5, i)
    i = i + 1
    Loop
    Close #fnum
    After that, I had to extract the useful columns (1 and 3) in this way:

    Code:
    ReDim bigArray(1 To UBound(myArr, 2))
    For i = 1 To UBound(myArr, 2)
    bigArray(i) = myArr(1, i) & " " & myArr(3, i) & " "
    Next i
    And, finally, we will have:

    Code:
    bigCities = Join(bigArray)
    Even the result is ok I feel the right approach should be other, a simpler one...
    For example, I think the last next-for looping could be, in a way, avoided. But how? Thank you in advance.

    Daniel
    Last edited by Siddharth Rout; Jul 21st, 2012 at 05:29 AM. Reason: Added Code Tags

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