Another question:
How to delete specified line in text file?
Thanks,
David
Printable View
Another question:
How to delete specified line in text file?
Thanks,
David
You'll need to read the whole file in, and then write it back out (with the exception of the line you don't want in the file).
Any other solution? I don't like this :blush:
You could always give up and move on to another project.Quote:
Originally Posted by manowar
:afrog:
Ok.
Than tell me this. If I load whole text from file. How can I than delete those line ?
Something like this should do...
Code:Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Dim sr As StreamReader
Dim sw As StreamWriter
Dim arrLines As New ArrayList
sr = New StreamReader(New FileStream("c:\somedoc.txt", FileMode.Open))
While sr.Peek <> (-1)
Dim s As String = sr.ReadLine
If s <> SomeCriteriaToDetermineThatItShouldBeDeleted() Then
arrLines.Add(s)
End If
End While
sr.Close()
sw = New StreamWriter(New FileStream("c:\somedoc.txt", FileMode.Create))
For Each s As String In arrLines
sw.WriteLine(s)
Next
sw.Close()
End Sub
If is there any other solution please let me know please.
What type of data is in the text file? If it is something that could be stored in an XML file then that might be a better option. .NET provides methods to deal with XML in an OO fashion. Text is just one long string and it's up to you to find the bit you don't want and get rid of it.
it normal txt file with text.
But what does it represent? Is it just a series of words, like a story or something, or does it have a well defined structure? If it is structured then XML may be a possibility. Otherwise, it's read in, find, delete, resave.Quote:
Originally Posted by manowar
if you use the find, delete, resave procedure then you might even want to consider the replace() function - might be a little simpler.
Read in the file into one string (i am assuming it's not big - watch out for overflow)
Replace(String, NotNeededStr, ReplaceWithNothing)
Delete file
Write back the whole string to the file
Why dont you like Crptcblades code example? It does what you asked.
They don't like me 'cause I'm black.Quote:
Originally Posted by RobDog888
:cry:
I thought you were an evil space elf with those pointy ears and yellow Don King hair?
Only on my mother's side ;)
crpt's code, nicely done =]