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

    Reading 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
    Sep 2001
    Location
    IL, USA
    Posts
    1,090

    Re: Reading from a file

    'You can do it by reading the whole file and put all the lines in an array
    'then you can use the array index to manipulate different lines.
    private Sub Command1_Click()
    'This code needs error handling.
    Dim FileName as string, retText as string, NumOfLines as Integer
    Dim Recds as Variant, Flds as Variant, i as Long, j as Long
    FileName = "C:\Test1.txt"
    Open FileName for binary as #1
    retText = input(FileLen(FileName), #1)
    Close #1
    'get all the records in the array Recds
    Recds = Split(retText, vbCrLf)
    NumOfLines = UBound(Recds)
    Debug.print UBound(Recds)
    If NumOfLines > 0 then
    'If you have an empty line at the end of the file then your last line is
    'Recds(NumOfLines - 1) other than that the last line is Recds(NumOfLines)
    Debug.print "Last line: "; Recds(NumOfLines - 1)
    End If
    End Sub


    Help us improve our answers by rating them.


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