Results 1 to 2 of 2

Thread: 1st line of file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    92

    1st line of file

    If i want to read a file from the top to bottom, but i dont wish to use EOF instead i want to read line by line ( i=i+1)
    The reason i wish to use that is ...maybe inside a file as below..

    ABC
    vb3=1
    Vb4=2
    vb5=3

    DEF
    Vb1=11
    VB2=99
    VB5=999

    GHI
    DEF
    Vb51=11
    VB2=77
    VB5=9993


    i want direct read the DEF and treat DEF as the first line of the file...
    How do i code it?
    please help

  2. #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