CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2003
    Posts
    16

    remove a line in file

    Dear Sir,

    I would like to remove 1 line in file (.txt).This line will in the last line of the file.I'm not sure how to do it. Please help me.

    Thanks

  2. #2
    Join Date
    May 2002
    Location
    Colombo,Sri Lanka
    Posts
    1,110
    I don't think u can delete a line from text file.

    I had same problem and i copy line by line to another text file by ignoring the line which I want to delete

  3. #3
    Join Date
    Nov 2003
    Location
    Seattle, WA
    Posts
    265
    The only way to do that to my knowledge is to read the whole file
    then rewrite it skipping the last line.

    Code:
    Private Sub RemoveLastLine(ByVal sFile As String)
        Dim fileData As Variant
        Dim temp As String
         
        Open sFile For Binary Access Read As #1
            temp = Space(LOF(1))
            Get #1, , temp
        Close #1
         
        fileData = Split(temp, vbCrLf)
         
        Open sFile For Output As #1
            For X = LBound(fileData) To UBound(fileData) - 1
                Print #1, fileData(X)
            Next
        Close #1
    End Sub

  4. #4
    Join Date
    May 2002
    Location
    Montreal
    Posts
    450
    Jinto's code seems to be doing exactly what you are looking for. Read in the whole file, and rewrite the file leaving out the last line.

    BTW, if you need to delete a specified line from a text file you can use the code in this link http://www.freevbcode.com/ShowCode.asp?ID=2667

    I used it and modified it to suit my own requirements.
    Cheers,
    Laurent

    For an aviator, the three best things in life are a good landing, a good orgasm, and a good sh*t. A night carrier landing is one of the few opportunities to experience all three at the same time.

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