PureSc0pe Posted March 26, 2004 Posted March 26, 2004 The first code writes the text to the end line, but if used more than once, it puts all of the added text on the same line. How do I make it write to its own line every time? Also, how can the second code be used to delete a line of text. I want to have to type the exact line that you want deleted for it to work. VB Code: Add to File Dim file As String Dim StreamWriter As New IO.StreamWriter(TextBox2.Text, True) StreamWriter.Write(TextBox3.Text) StreamWriter.Close() VB Code: Delete from File Dim file As String Dim StreamWriter As New IO.StreamWriter(TextBox2.Text) StreamWriter.Write(TextBox4.Text) StreamWriter.Close() Quote It's not impossible, it's Inevitable.
TechnoTone Posted March 26, 2004 Posted March 26, 2004 See WriteLine. Quote TT (*_*) There are 10 types of people in this world; those that understand binary and those that don't.
Leaders Iceplug Posted March 26, 2004 Leaders Posted March 26, 2004 With Sequential File Access streams, you don't actually "delete" a line from a file. You just "conveniently forget to put it back in the file". You'd want to have a Stream for Reading and then one for Writing. You can use a StringBuilder also. (something like this) Caption = SR.Readline() [color=Teal]'Read a line from file.[/color] [color=Blue]If[/color] Caption <> "asdfghjkl" [color=Blue]Then[/color] SB.Append(Caption) SB.Append(System.Environment.NewLine) [color=Teal]'Conveniently forget to write the line "asdfghjkl" back to file.[/color] [color=Blue]End If Loop[/color] SW.Write(SB.ToString()) [color=Teal]'Write the built string to file.[/color] Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
PureSc0pe Posted March 26, 2004 Author Posted March 26, 2004 (edited) I've got most of what you've said here, but SB? I assume that is Stringbuilder as you mentioned earlier. However, StringBuilder is not showing up as a Variable or whatever. Anything in Bold has an error. VB Code: Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim file As String Dim SR As New IO.StreamReader(TextBox2.Text) Dim SW As New IO.StreamWriter(TextBox2.Text) Dim SB As StringBuilder file = (TextBox2.Text) file = SR.ReadLine() If file <> (TextBox4.Text) Then SB.Append(file) SB.Append(System.Environment.NewLine) End If Loop SW.Write(SB.ToString()) End Sub Edited March 26, 2004 by PureSc0pe Quote It's not impossible, it's Inevitable.
Leaders Iceplug Posted March 26, 2004 Leaders Posted March 26, 2004 [color=Blue]Dim[/color] SB [color=Blue]As[/color] System.Text.StringBuilder = [color=Blue]New[/color] System.Text.StringBuilder() Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
PureSc0pe Posted March 26, 2004 Author Posted March 26, 2004 The Code shows no errors, but when I run the application it says: An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll. Additional information: The process cannot access the file "C:\list.txt" because it is being used by another process. It highlights Dim SW As New IO.StreamWriter(TextBox2.text) as the problem. Is it saying that because it's being used by StreamReader? If so, how would I fix that? VB Code: Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim file As String Dim SR As New IO.StreamReader(TextBox2.Text) Dim SW As New IO.StreamWriter(TextBox2.Text) Dim SB As System.Text.StringBuilder = New System.Text.StringBuilder file = (TextBox2.Text) file = SR.ReadLine() If file <> (TextBox4.Text) Then SB.Append(file) SB.Append(System.Environment.NewLine) End If SW.Write(SB.ToString()) End Sub Quote It's not impossible, it's Inevitable.
Leaders Iceplug Posted March 26, 2004 Leaders Posted March 26, 2004 Oh, yeah, you need to close the streamreader before you can open the streamwriter. So, your code will look something like this. Dim SR As IO.StreamReader = New StreamReader(...) Dim SW As IO.StreamWriter SR.Close() SW = New StreamWriter(...) SW.Write(...) SW.Close() Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
PureSc0pe Posted March 26, 2004 Author Posted March 26, 2004 Ok, when I run it, it deletes everything from the file except the first line. I need it to keep everything except what is typed into the texbox. VB Code: Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim file As String Dim SR As New IO.StreamReader(TextBox2.Text) file = (TextBox2.Text) file = SR.ReadLine() SR.Close() Dim SW As New IO.StreamWriter(TextBox2.Text) Dim SB As System.Text.StringBuilder = New System.Text.StringBuilder If file <> (TextBox4.Text) Then SB.Append(file) SB.Append(System.Environment.NewLine) End If SW.Write(SB.ToString()) SW.Close() End Sub Quote It's not impossible, it's Inevitable.
AFterlife Posted March 27, 2004 Posted March 27, 2004 :D PureScope is going to be the Master of File I/O! :D Quote
PureSc0pe Posted March 27, 2004 Author Posted March 27, 2004 :D PureScope is going to be the Master of File I/O! :D :D Hope so, but for now I still need help on what I said in my last post. Quote It's not impossible, it's Inevitable.
Administrators PlausiblyDamp Posted March 27, 2004 Administrators Posted March 27, 2004 You are opening your streamreader (SR) but then closing it after only 1 Readline. you would really need to do this in a loop. Try something like the following (not tested though so no promises) Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim line As String = "" Dim SR As New IO.StreamReader(TextBox2.Text) Dim SB As System.Text.StringBuilder = New System.Text.StringBuilder Do line = SR.ReadLine If line <> TextBox4.Text Then SB.Append(line) SB.Append(System.Environment.NewLine) End If Loop Until line = "" SR.Close() Dim SW As New IO.StreamWriter(TextBox2.Text, False) SW.Write(SB.ToString()) SW.Close() End Sub Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
PureSc0pe Posted March 27, 2004 Author Posted March 27, 2004 That worked great, thanks for all your help. :D Quote It's not impossible, it's Inevitable.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.