Results 1 to 7 of 7

Thread: [RESOLVED] Help needed to read text file

  1. #1

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Resolved [RESOLVED] Help needed to read text file

    Hi all,

    I need to read the last line alone from a text file and display it to user.

    Please help me!

    Thanks in advance.

  2. #2
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Help needed to read text file

    This is a solution, it is fine if the textfile is not huge, i'm sure someone will post a more effecient solution...

    VB Code:
    1. Dim strBuffer$
    2. Dim strLine$
    3. Dim strLineArray$()
    4. Dim strLastLine$
    5.  
    6.  
    7. Open "c:\t.txt" For Input As #1
    8.     While Not EOF(1)
    9.         Line Input #1, strLine
    10.         strBuffer = strBuffer & vbCrLf & strLine
    11.     Wend
    12. Close #1
    13.  
    14. strLineArray = Split(strBuffer, vbCrLf)
    15. strLastLine = strLineArray(UBound(strLineArray))
    16.  
    17. MsgBox "the last line is: " & strLastLine
    Chris

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,651

    Re: Help needed to read text file

    Can't remember if this is quite right, but something along these lines would be a lot quicker:
    VB Code:
    1. Dim ff As Long, all As String
    2.  
    3. ff = FreeFile()
    4. Open filename for Binary Lock Write As #ff
    5. all = Space(LOF(ff))
    6. Get #ff, , all
    7. Close #ff
    8.  
    9. Dim lastLine as String
    10. lastLine = Mid$(all, InStrRev(all, vbNewLine), Len(all))
    11.  
    12. MsgBox "Last line is: " & lastLine

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,692

    Re: Help needed to read text file

    The last character may be vbNewLine. If the text from that point is "", test again starting at the last point.

    BTW, Mid$(string, start) reads to the end of the string.

  5. #5
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Help needed to read text file

    Quote Originally Posted by Al42
    The last character may be vbNewLine. If the text from that point is "", test again starting at the last point.

    BTW, Mid$(string, start) reads to the end of the string.
    if the last char is a new line then post #3 will say

    'The last line is:'
    Chris

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,651

    Re: Help needed to read text file

    Which would be the correct result.

  7. #7

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Help needed to read text file

    Hi,

    I found another way to solve this problem. Here is the code:
    VB Code:
    1. Public Function fnReadLastLine(strFileName As String)
    2.     'You can call this function as:
    3.     'Call fnReadLastLine("C:\a12.txt")
    4.     Dim fPos As Long, LastLine As String, aByte As Byte
    5.     Dim testCRLF As String * 2
    6.     Const LF = 10
    7.  
    8.     On Error GoTo fnReadLastLine_Error
    9.  
    10.     Open strFileName For Binary As #1
    11.     fPos = LOF(1) + 1
    12.     Do
    13.         fPos = fPos - 2
    14.         Seek #1, fPos
    15.         Get #1, , testCRLF
    16.     Loop While testCRLF = vbCrLf
    17.     Do
    18.         fPos = fPos - 1
    19.         Seek #1, fPos
    20.         Get #1, , aByte
    21.     Loop Until aByte = LF
    22.     Line Input #1, LastLine
    23.     Close #1
    24.     MsgBox Trim$(LastLine)
    25.  
    26. ExitHere:
    27.     On Error GoTo 0
    28.     Exit Function
    29.  
    30. fnReadLastLine_Error:
    31.     MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure fnReadLastLine of Module Module1"
    32. End Function

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