CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2002
    Posts
    12

    How to read the last line first from a file???

    Hi,

    Is is possible to first read the last line in the file and then read from the first line thru last line - 1?

    I need the info that is stored at the last line in the file and I need that before I get any data and do any other manipulation.

    Thank you,

    Regards,
    Swati


  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: How to read the last line first from a file???


    Here are cople of ways to read the last line

    Here's a simple way - it uses the 'seek' command in VB to go to an assigned distance from the end
    of the file, then reads in the rest of the file and looks for the 'VBCRLF' :

    private Function GetLastLine(byval FileName as string) as string
    Dim iFile as Integer
    Dim lFileLen as Long
    Dim sBuffer as string
    Dim iPos as Integer
    '
    ' You might need to play with the offset depending on how big
    ' your 'lines' usually are in the file
    '
    Const OFFSET as Long = 160
    '
    ' The line delimiter string - in this case I'm using VBCRLF, it
    ' could just be LF in some cases, again, be careful
    '
    Const FINDSTR as string = vbCrLf
    '
    iFile = FreeFile
    sBuffer = Space$(OFFSET)
    '
    Open FileName for binary Access Read as #iFile
    seek #iFile, (LOF(iFile) - OFFSET)
    get #iFile, , sBuffer
    Close #iFile
    '
    iPos = InStrRev(sBuffer, FINDSTR)
    If iPos = 0 then
    GetLastLine = sBuffer
    else
    GetLastLine = mid$(sBuffer, iPos + len(FINDSTR))
    End If
    '
    End Function


    'another example using RichTextBox
    private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (byval hwnd as Long, byval wMsg as Long, byval wParam as Long, byval lParam as Any) as Long
    Const EM_LINEINDEX = &HBB ' get offset of current line


    private Sub Command1_Click()
    Dim X
    rtf.LoadFile "C:\Autoexec.Bat"
    rtf.SelStart = len(rtf.Text) - 1
    rtf.Refresh
    X = rtf.SelStart - SendMessage(rtf.hwnd, EM_LINEINDEX, -1, 0&) + 1
    ' display last line of text file
    MsgBox mid(rtf.Text, len(rtf.Text) - X)
    End Sub

    private Sub rtf_SelChange()
    txtRow = rtf.GetLineFromChar(rtf.SelStart) + 1 ' get the current line
    txtcol = rtf.SelStart - SendMessage(rtf.hwnd, EM_LINEINDEX, -1, 0&) + 1
    End Sub


    But I think it is not what you need. You need to count number of lines in the file and then read all of them but last.




    Iouri Boutchkine
    iouri@hotsheet.com
    Iouri Boutchkine
    iouri@hotsheet.NOSPAM.com

  3. #3
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: How to read the last line first from a file???

    Here is another solution


    Dim A$, S$()
    Open temp for input as #1
    A = input(LOF(1), #1)
    Close #1
    S = Split(A, vbCrLf)
    ReDim Preserve S(UBound(S) - 1)

    'now you have all the lines in the array except the last one
    'you can write it to the file
    Open temp for Output as #1
    print #1, Join(S, vbCrLf)
    Close #1




    Iouri Boutchkine
    iouri@hotsheet.com
    Iouri Boutchkine
    iouri@hotsheet.NOSPAM.com

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

Featured