[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:
Dim file As New FileStream(data_path & "notes.txt", FileMode.Open)
Dim text As String = Nothing
Dim reader As New StreamReader(file)
Dim writer As New StreamWriter(file)
While Not reader.EndOfStream
text = reader.ReadLine
If text.Contains(value) Then
text.Replace(value, "")
writer.Write(text)
End If
End While
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
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.
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 :)
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.
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.
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.
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:
Dim file As New FileStream(data_path & "notes.txt", FileMode.Open)
Dim text As String = Nothing
Dim reader As New StreamReader(file)
Dim writer As New StreamWriter(file)
text = reader.ReadToEnd
If text.Contains(value) Then
text.Replace(value, "")
End If
writer.Write(text)
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".
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.
Re: [2005] Removing a line of text from a .txt file
I've already been there...
but I'll give it another look.
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:
Dim text As Array
Dim lines As New List(Of String)
text = File.ReadAllLines(data_path & "notes.txt")
lines.AddRange(text)
Array.Clear(text, 0, text.Length)
lines.RemoveAt(lines.IndexOf(value))
text = lines.ToArray()
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. :)
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.
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!!!!!!!!!!!!!! :confused:
VB Code:
Private Sub RemoveLines(ByVal fileName As String, ByVal linesToRemove() As String)
Dim lines() As String = IO.File.ReadAllLines(fileName)
Using sw As New IO.StreamWriter(fileName)
For Each line As String In lines
If Array.IndexOf(linesToRemove, line) = -1 Then
sw.WriteLine(line)
End If
Next
End Using
End Sub
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!!!!!!!!!!!!!! :confused:
VB Code:
Private Sub RemoveLines(ByVal fileName As String, ByVal linesToRemove() As String)
Dim lines() As String = IO.File.ReadAllLines(fileName)
Using sw As New IO.StreamWriter(fileName)
For Each line As String In lines
If Array.IndexOf(linesToRemove, line) = -1 Then
sw.WriteLine(line)
End If
Next
End Using
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 :)
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())