Results 1 to 14 of 14

Thread: [RESOLVED] [2005] Removing a line of text from a .txt file

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2007
    Location
    Portugal
    Posts
    51

    Resolved [RESOLVED] [2005] Removing a line of text from a .txt file

    Hi all

    So...I am writing an app that writes notes, inserted by the user, to a txt file. The app saves that notes to a txt file, and when opening it reads the file and shows the already inserted notes.
    However, I want the user to be able to delete some of those notes...but I can't seem to do it...this is what I have:

    VB Code:
    1. Dim file As New FileStream(data_path & "notes.txt", FileMode.Open)
    2.         Dim text As String = Nothing
    3.         Dim reader As New StreamReader(file)
    4.         Dim writer As New StreamWriter(file)
    5.  
    6.         While Not reader.EndOfStream
    7.             text = reader.ReadLine
    8.             If text.Contains(value) Then
    9.                 text.Replace(value, "")
    10.                 writer.Write(text)
    11.             End If
    12.         End While
    13.  
    14.         file.Close()

    *value is the string/note to be replaced
    *data_path is the path I want...no problems here


    It goes fine, but the text.Replace(value, "") line doesn't seem to work.
    I've also tried text.Remove(0) but it doesn't removes the line...it looks like it has no effect.



    Hope you guys can help me.
    Thanks in advance
    Last edited by buttpt; Feb 24th, 2007 at 03:45 PM.

  2. #2
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: [2005] Removing a line of text from a .txt file

    I know nothing about this sort of stuff, but right off the bat, I don't think it is a good idea to be both reading from AND writing to the same file. I really don't think that will work. Change your writer to point to a temp file and then save the temp file over the original file when you are done. Thats my best guess.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2007
    Location
    Portugal
    Posts
    51

    Re: [2005] Removing a line of text from a .txt file

    i've tried that too. It didn't work...nothing happens to the text when I try to replace/remove it...so when I did that it just saved my value to the temp text file.
    thanks for answering

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Removing a line of text from a .txt file

    What you want to do is:
    1. Open the file and read the whole file content to a string variable.
    2. Replace the text to be deleted with nothing
    3. Write the modified text back to the file.

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2007
    Location
    Portugal
    Posts
    51

    Re: [2005] Removing a line of text from a .txt file

    exactly...but the problem is that I don't know how to replace the text to be deleted...tried replace and remove, but nothing happened.

  6. #6
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2005] Removing a line of text from a .txt file

    I would read every single line via a loop and just skip the line you want to remove. If you haven't fiqured it out by now, read my sig.

  7. #7

    Thread Starter
    Member
    Join Date
    Feb 2007
    Location
    Portugal
    Posts
    51

    Re: [2005] Removing a line of text from a .txt file

    skip the line? I don't get it...I followed the code with a breakpoint and the line is not skipped...and the program goes through the replace part but doesn't replace nothing.
    If the line read contains my value, then replace that value...this is how I read it...but I'm probably wrong.
    this is what I was trying now...without the loop

    VB Code:
    1. Dim file As New FileStream(data_path & "notes.txt", FileMode.Open)
    2.         Dim text As String = Nothing
    3.         Dim reader As New StreamReader(file)
    4.         Dim writer As New StreamWriter(file)
    5.  
    6.         text = reader.ReadToEnd
    7.         If text.Contains(value) Then
    8.             text.Replace(value, "")
    9.         End If
    10.  
    11.         writer.Write(text)
    12.         writer.Close()

    The text is "Hello Goodbye"...after the replace it stays the same, so when it writes to the file I get "Hello Goodbye Hello Goodbye".

  8. #8
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Removing a line of text from a .txt file

    There are many examples of removing lines from a text file in this forum and searching will give you some answers. Check the post #5.

  9. #9

    Thread Starter
    Member
    Join Date
    Feb 2007
    Location
    Portugal
    Posts
    51

    Re: [2005] Removing a line of text from a .txt file

    I've already been there...
    but I'll give it another look.

  10. #10

    Thread Starter
    Member
    Join Date
    Feb 2007
    Location
    Portugal
    Posts
    51

    Re: [2005] Removing a line of text from a .txt file

    ok I did it...with a different method.
    here's how...

    Quote Originally Posted by jmcilhinney
    Read the file into an array (File.ReadAllLines). Load the array into a collection (New List(Of String)). Remove the lines you don't want (List.RemoveAt). Extract an array from the collection (List.ToArray). Write the array to the file (File.WriteAllLines).
    VB Code:
    1. Dim text As Array
    2.         Dim lines As New List(Of String)
    3.  
    4.         text = File.ReadAllLines(data_path & "notes.txt")
    5.         lines.AddRange(text)
    6.         Array.Clear(text, 0, text.Length)
    7.         lines.RemoveAt(lines.IndexOf(value))
    8.         text = lines.ToArray()
    9.         file.WriteAllLines(data_path & "notes.txt", text)


    However, I still don't understand why the first method didn't work.
    Anyway...thanks all for the help.

  11. #11
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: [2005] Removing a line of text from a .txt file

    It is not working because you are not saving the filestream back to the file. Not the best approach for text but you can still search for a way to save a filestream to a file once you are done changing the contents of the stream. Also name your stream myFileStream or so instead of 'file' to avoid confusion with the system.io.file class.
    VB 2005, Win Xp Pro sp2

  12. #12
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Removing a line of text from a .txt file

    Quote Originally Posted by buttpt
    I've already been there...
    but I'll give it another look.
    Well, I don't know how posibly this method from the link can NOT work!!!!!!!!!!!!!!
    VB Code:
    1. Private Sub RemoveLines(ByVal fileName As String, ByVal linesToRemove() As String)
    2.         Dim lines() As String = IO.File.ReadAllLines(fileName)
    3.         Using sw As New IO.StreamWriter(fileName)
    4.             For Each line As String In lines
    5.                 If Array.IndexOf(linesToRemove, line) = -1 Then
    6.                     sw.WriteLine(line)
    7.                 End If
    8.             Next
    9.         End Using
    10.     End Sub

  13. #13

    Thread Starter
    Member
    Join Date
    Feb 2007
    Location
    Portugal
    Posts
    51

    Re: [2005] Removing a line of text from a .txt file

    Quote Originally Posted by VBDT
    Well, I don't know how posibly this method from the link can NOT work!!!!!!!!!!!!!!
    VB Code:
    1. Private Sub RemoveLines(ByVal fileName As String, ByVal linesToRemove() As String)
    2.         Dim lines() As String = IO.File.ReadAllLines(fileName)
    3.         Using sw As New IO.StreamWriter(fileName)
    4.             For Each line As String In lines
    5.                 If Array.IndexOf(linesToRemove, line) = -1 Then
    6.                     sw.WriteLine(line)
    7.                 End If
    8.             Next
    9.         End Using
    10.     End Sub
    I didn't say that it doesn't work. I said "I've been there...". I read the topic but didn't try that part of code. Also, I wanted to understand why my code wasn't working so...I don't usually copy and paste code...I like to create my own methods.
    Anyway, the problem is solved now. Thanks to all

  14. #14
    New Member
    Join Date
    Mar 2011
    Location
    Binh Giang - Hai Duong - Viet Nam
    Posts
    1

    Re: [RESOLVED] [2005] Removing a line of text from a .txt file

    Code:
    Import System.IO
    
    Dim linesList As New List(Of String)(File.ReadAllLines("filePath"))
    
    'Remove the line to delete, e.g.
    linesList.RemoveAt(1)
    
    File.WriteAllLines("filePath", linesList.ToArray())

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