Results 1 to 2 of 2

Thread: 1st line of file

Threaded View

  1. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: 1st line of file

    To read file "line-by-line" you have to either use loop and check while you didn't reach EOF or read entire text into string variable (this is already an overhead) then load array and then loop through array:
    Code:
    'method 1 (better by far)
    Dim strLine$
    
        Open somefile For Input As #1
            Do While Not EOF(1)
                Line Input #1, strLine
                If InStr(1, UCase(strLine), "DEF") > 0 Then
                    'do something
                End If
            Loop
        Close #1
        
    'method 2 (will also work but isn't necessary best approach)
    Dim strText$, arLines() As String, i%
    
        Open somefile For Input As #1
            strText = Input(LOF(1), #1)
            arLines = Split(strText, vbNewLine)
            strText = ""
            
            For i = 0 To UBound(arLines)
                strLine = arLines(i)
                If InStr(1, UCase(strLine), "DEF") > 0 Then
                    'do something
                End If
            Next i
        Close #1

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