Results 1 to 4 of 4

Thread: Read Text File Line-By-Line

  1. #1

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375

    Question

    I need to take a text file, read it line by line, and put certain lines into an array...

    I need some help on the reading line by line part...

    Can anyone steer me in the right direction? I don't need to read it all into an array, just 1 line at a time, and if the line meets certain criteria, put THAT LINE in an array...(I can handle the "if it meets certain criteria" part...)

    Thanks!!
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  2. #2
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    Something along the lines of.

    Code:
    Open "myTxt.txt" For Input As #1
    
    Do While Not Eof(1)
      Line Input #1, myVariabe
    Loop
    
    Close #1
    Hope that helps.
    Iain, thats with an i by the way!

  3. #3
    Hyperactive Member mikef's Avatar
    Join Date
    Jun 2000
    Location
    Beach bound...
    Posts
    510
    Or you could put it directly into a Perfect Fitting array like so:

    Dim ln$(10)

    lines = 0: Erase ln$: maxlines = 10:

    Do While Not EOF(1)
    Call createnextline((maxlines))
    Line Input #1, ln$(lines)
    ln$(lines) = RTrim$(ln$(lines))
    Loop


    Public Sub createnextline(maxlines)

    lines = lines + 1

    If lines + 20 >= maxlines Then
    maxlines = lines + 40
    ReDim Preserve ln$(maxlines)
    End If

    End Sub


    Good luck...

  4. #4
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    "or"

    or.....'the results are what you want.

    'list box is only for a visula of what is happening
    'you can remove it when satisfied you get what
    'you are after


    Option Explicit
    Option Compare Text


    Dim filesys, txtStream As Object


    Private Sub Form_Load()
    Dim myArr() As Variant
    Dim i As Integer
    Set filesys = CreateObject("Scripting.FileSystemObject")
    Set txtStream = filesys.openTextFile("C:\my documents\x.txt")
    Do Until txtStream.atendofstream
    i = i + 1
    ReDim Preserve myArr(1 To i) As Variant
    myArr(i) = txtStream.readline
    myArr(i) = Trim(myArr(i))
    If myArr(i) = "Whatever line you want to look for" Then
    List1.AddItem myArr(i)
    End If

    Loop
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

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