|
-
Feb 23rd, 2007, 04:47 PM
#1
Thread Starter
Member
[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
Last edited by buttpt; Feb 24th, 2007 at 03:45 PM.
-
Feb 23rd, 2007, 04:53 PM
#2
Fanatic Member
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..."
-
Feb 23rd, 2007, 06:30 PM
#3
Thread Starter
Member
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
-
Feb 23rd, 2007, 08:01 PM
#4
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.
-
Feb 24th, 2007, 07:16 AM
#5
Thread Starter
Member
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.
-
Feb 24th, 2007, 09:03 AM
#6
Frenzied Member
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.
-
Feb 24th, 2007, 09:50 AM
#7
Thread Starter
Member
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".
-
Feb 24th, 2007, 11:21 AM
#8
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.
-
Feb 24th, 2007, 12:18 PM
#9
Thread Starter
Member
Re: [2005] Removing a line of text from a .txt file
I've already been there...
but I'll give it another look.
-
Feb 24th, 2007, 01:04 PM
#10
Thread Starter
Member
Re: [2005] Removing a line of text from a .txt file
ok I did it...with a different method.
here's how...
 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.
-
Feb 24th, 2007, 01:55 PM
#11
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.
-
Feb 24th, 2007, 02:07 PM
#12
Re: [2005] Removing a line of text from a .txt file
 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:
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
Last edited by VBDT; Feb 24th, 2007 at 02:14 PM.
-
Feb 24th, 2007, 03:44 PM
#13
Thread Starter
Member
Re: [2005] Removing a line of text from a .txt file
 Originally Posted by VBDT
Well, I don't know how posibly this method from the link can NOT work!!!!!!!!!!!!!!
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
-
Mar 1st, 2011, 03:00 AM
#14
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|