|
-
Jul 29th, 2009, 12:21 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Remove line from text file
I have text file that need to read until *+*+* Top *+*+*
and write the content until *+*+* Top *+*+* but i dont want to write *+*+* Top *+*+* this line
This is my code so far
Code:
Dim myReader As StreamReader = New StreamReader("C:\temp2.txt")
Dim myLine As String
Dim fc As StreamWriter = File.CreateText("C:\kaya1.txt")
Dim sf As StreamWriter = File.CreateText("C:\kaya.txt")
Do
myLine = myReader.ReadLine()
fc.WriteLine(myLine)
If myLine.Contains("*+*+* Top *+*+*") Then
myLine.Replace("*+*+* Top *+*+*", "")
End If
Loop Until (myLine.Contains("*+*+* Top *+*+*"))
sf.Write(myReader.ReadToEnd())
myReader.Close()
myReader = Nothing
sf.Close()
fc.Close()
When i use Replace function to replace that content with space, it still write that line. And I dont know how to fix this problem?
-
Jul 29th, 2009, 12:33 AM
#2
Re: Remove line from text file
Have a look at your code and ask yourself what it actually does:
Code:
myLine = myReader.ReadLine()
fc.WriteLine(myLine)
If myLine.Contains("*+*+* Top *+*+*") Then
myLine.Replace("*+*+* Top *+*+*", "")
End If
It reads the line from one file, then writes it to another, then tests whether it contains the text, then replaces the text. What's the point of testing the line for that text and replacing it if you've already written it to the second file? Now, ask yourself what you actually want to happen. You want to read the line, test if it contains the text and then only write it to the second file If it does Not contain the text.
When you actually write down what you want to happen, isn't it then easier to write code to do it? If you don't even know what steps your code is supposed to be executing then what chance it will execute those steps?
-
Jul 29th, 2009, 12:35 AM
#3
Fanatic Member
Re: Remove line from text file
Hey kayatri... You could read each line, check the text and then write it to another file. If it's the string to you want it to stop at, then it should exit the writer. Another way would be to store it into an array and check, then copy.
-
Jul 29th, 2009, 12:55 AM
#4
Thread Starter
Addicted Member
Re: Remove line from text file
thanx tassa and jimchilhinney i solved it with ur help
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
|