Hi all!!
What I am trying to do today is write to a textfile then read from the file get some lines from that file and edit them. and re-write them to the file. Not replace I want the edited lines at the end of the file. This is all being done while the file is still open. Below is my code. I have notated it to show what I am doing.
What is happening is the length of "line#" is not increasing and not sure why not.
[code=vbnet]
' Declarations
Dim sw As StreamWriter
Dim sr As StreamReader
Dim path1 As String = "Showme.Txt "
Dim longest As Integer
Dim length1 As Integer
Dim length2 As Integer
Dim length3 As Integer
Dim length4 As Integer
Dim length5 As Integer
Dim line1 As String = AddBox1.Text
Dim line2 As String = AddBox2.Text
Dim line3 As String = AddBox3.Text
Dim line4 As String = AddBox4.Text
Dim line5 As String = AddBox5.Text
length1 = line1.Length
length2 = line2.Length
length3 = line3.Length
length4 = line4.Length
length5 = line5.Length
Dim addspace As String = " "
' Create and write to text file
sw = File.CreateText (path1)
sw.WriteLine("W elcome to Show Me")
sw.WriteLine()
sw.WriteLine("H ere we will take the numbers you have entered and show you how to add them to get an answer.")
sw.WriteLine()
sw.WriteLine("F irst Lets take the sumbers you entered")
sw.WriteLine()
'Checks to see if an entry was made in each textbox and writes
'to file only if true. Does this for all 5 textboxes
If length1 > 0 Then
sw.WriteLine((V al(AddBox1.Text )))
End If
If length2 > 0 Then
sw.WriteLine((V al(AddBox2.Text )))
End If
If length3 > 0 Then
sw.WriteLine((V al(AddBox3.Text )))
End If
If length4 > 0 Then
sw.WriteLine((V al(AddBox4.Text )))
End If
If length5 > 0 Then
sw.WriteLine((V al(AddBox5.Text )))
End If
sw.WriteLine("N ow we need to line up the last digit of all your numbers")
' Checks the length of each string and sets "longest"
' to the length of the longest string
If length1 > length2 And length1 > length3 And length1 > length4 And _
length1 > length5 Then
longest = length1
ElseIf length2 > length1 And length2 > length3 And length2 > length4 And _
length2 > length5 Then
longest = length2
ElseIf length3 > length1 And length3 > length2 And length3 > length4 And _
length3 > length5 Then
longest = length3
ElseIf length4 > length1 And length4 > length3 And length4 > length2 And _
length4 > length5 Then
longest = length4
ElseIf length5 > length2 And length5 > length3 And length5 > length4 And _
length5 > length1 Then
longest = length5
End If
' Insert a space at the beginning of the string for each
' instance of a string until all strings are the same length
' this will line up the last digits of each entry
Do Until length1 = longest
line1.Insert(1, addspace) ' ARRRRHG!!! now what?
length1 = line1.Length
Loop
'Writes the new string if it is numeric
If (IsNumeric(line 1)) Then
sw.WriteLine(li ne1)
End If
Do Until length2 = longest
line2.Insert(1, addspace)
length2 = line2.Length
Loop
If (IsNumeric(line 2)) Then
sw.WriteLine(li ne2)
End If
Do Until length3 = longest
line3.Insert(1, addspace)
length3 = line3.Length
Loop
If (IsNumeric(line 3)) Then
sw.WriteLine(li ne3)
End If
Do Until length4 = longest
line4.Insert(1, addspace)
length4 = line4.Length
Loop
If (IsNumeric(line 4)) Then
sw.WriteLine(li ne4)
End If
Do Until length5 = longest
line5.Insert(1, addspace)
length4 = line4.Length
Loop
If (IsNumeric(line 5)) Then
sw.WriteLine(li ne5)
End If
sw.Flush()
sw.Close()
[/code]
Any help would be greatly appreciated.
What I am trying to do today is write to a textfile then read from the file get some lines from that file and edit them. and re-write them to the file. Not replace I want the edited lines at the end of the file. This is all being done while the file is still open. Below is my code. I have notated it to show what I am doing.
What is happening is the length of "line#" is not increasing and not sure why not.
[code=vbnet]
' Declarations
Dim sw As StreamWriter
Dim sr As StreamReader
Dim path1 As String = "Showme.Txt "
Dim longest As Integer
Dim length1 As Integer
Dim length2 As Integer
Dim length3 As Integer
Dim length4 As Integer
Dim length5 As Integer
Dim line1 As String = AddBox1.Text
Dim line2 As String = AddBox2.Text
Dim line3 As String = AddBox3.Text
Dim line4 As String = AddBox4.Text
Dim line5 As String = AddBox5.Text
length1 = line1.Length
length2 = line2.Length
length3 = line3.Length
length4 = line4.Length
length5 = line5.Length
Dim addspace As String = " "
' Create and write to text file
sw = File.CreateText (path1)
sw.WriteLine("W elcome to Show Me")
sw.WriteLine()
sw.WriteLine("H ere we will take the numbers you have entered and show you how to add them to get an answer.")
sw.WriteLine()
sw.WriteLine("F irst Lets take the sumbers you entered")
sw.WriteLine()
'Checks to see if an entry was made in each textbox and writes
'to file only if true. Does this for all 5 textboxes
If length1 > 0 Then
sw.WriteLine((V al(AddBox1.Text )))
End If
If length2 > 0 Then
sw.WriteLine((V al(AddBox2.Text )))
End If
If length3 > 0 Then
sw.WriteLine((V al(AddBox3.Text )))
End If
If length4 > 0 Then
sw.WriteLine((V al(AddBox4.Text )))
End If
If length5 > 0 Then
sw.WriteLine((V al(AddBox5.Text )))
End If
sw.WriteLine("N ow we need to line up the last digit of all your numbers")
' Checks the length of each string and sets "longest"
' to the length of the longest string
If length1 > length2 And length1 > length3 And length1 > length4 And _
length1 > length5 Then
longest = length1
ElseIf length2 > length1 And length2 > length3 And length2 > length4 And _
length2 > length5 Then
longest = length2
ElseIf length3 > length1 And length3 > length2 And length3 > length4 And _
length3 > length5 Then
longest = length3
ElseIf length4 > length1 And length4 > length3 And length4 > length2 And _
length4 > length5 Then
longest = length4
ElseIf length5 > length2 And length5 > length3 And length5 > length4 And _
length5 > length1 Then
longest = length5
End If
' Insert a space at the beginning of the string for each
' instance of a string until all strings are the same length
' this will line up the last digits of each entry
Do Until length1 = longest
line1.Insert(1, addspace) ' ARRRRHG!!! now what?
length1 = line1.Length
Loop
'Writes the new string if it is numeric
If (IsNumeric(line 1)) Then
sw.WriteLine(li ne1)
End If
Do Until length2 = longest
line2.Insert(1, addspace)
length2 = line2.Length
Loop
If (IsNumeric(line 2)) Then
sw.WriteLine(li ne2)
End If
Do Until length3 = longest
line3.Insert(1, addspace)
length3 = line3.Length
Loop
If (IsNumeric(line 3)) Then
sw.WriteLine(li ne3)
End If
Do Until length4 = longest
line4.Insert(1, addspace)
length4 = line4.Length
Loop
If (IsNumeric(line 4)) Then
sw.WriteLine(li ne4)
End If
Do Until length5 = longest
line5.Insert(1, addspace)
length4 = line4.Length
Loop
If (IsNumeric(line 5)) Then
sw.WriteLine(li ne5)
End If
sw.Flush()
sw.Close()
[/code]
Any help would be greatly appreciated.
Comment