Using streamreader to open text as binary array? How?
im using the ReadLine command to read text hawever it is not reading characters corectly.
For example it reads "Ðeãdßáñg" as "edg". skipping the Ð, ã, and ßáñ characters.
One person suggests that I use streamreader to open the text as a binary array. How would I do this? What code should I add?
How can I resolve this problem?
here is a copy of my code:
Code:
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
Dim myString As String
Dim input As String
Dim iCount As Integer
Dim theFile As FileInfo
Dim re As StreamReader
Dim time As Object
Label2.Text = TimeOfDay
'Open the file to a stream
theFile = New FileInfo("C:\Program Files\Novalogic\Delta Force Land Warrior\scores.txt")
re = theFile.OpenText()
'Read in the first line
input = re.ReadLine
'Loop through each line
Do
iCount = iCount + 1
'This will let the first four lines go, then
'it will add the rest of the file to myString.
If iCount > 4 Then
'You can put a return character inbetween these two if you
'want to have a new line for each line added.
myString = myString & vbCrLf & input & " " & Now
RichTextBox1.Text = myString.ToString()
End If
'Read in the next line for the loop.
input = re.ReadLine.
If input = "" Then
iCount = -1
End If
Loop Until input = "End"
Dim fs As FileStream
Dim sw As StreamWriter
fs = New FileStream("c:\DoneStats.txt", FileMode.Append)
sw = New StreamWriter(fs)
Dim itm As Object
For Each itm In RichTextBox1.Text
sw.Write(itm)
re.Close()
Next
re.Close()
sw.Close()
fs.Close()
Dim fs2 As FileStream
Dim sw2 As StreamWriter
fs2 = New FileStream("C:\Program Files\Novalogic\Delta Force Land Warrior\scores.txt", FileMode.Create)
sw2 = New StreamWriter(fs2)
End Sub
Im having one heck of a time integrating this code into my existing code. I cant seem to get it to work right. Can i utilize the binaryreader to scan one line of text at a time?
First of all, thanx for your help on this Edneeis.
It works. I have one question. How can I get it to read one line at a time so that I can control which lines of text are displayed in the textbox. In the textfile I use, I will need to delete several diffent lines of text. In my original code i use Input = re.readline and count each line so that I can delete the header of each section of text. I have attatched a sample copy if the texte file. after running the program It should look like this:
Once the data is read into the string you can split it by line (controlchars.newline). Here is what I mean:
VB Code:
'previous code goes here
Dim lines() As String
lines = Split(str, ControlChars.NewLine)
Me.Text = lines.GetUpperBound(0)
That makes each line an element of the lines array and you can do whatever you need to with it from there. The example just puts the line count in the caption of the form.
are lines broken by count? for instance I always need the first 4 lines deleted(or not read into the textbox), and after that I need any 4 lines that begin with 4 specific characters deleted( or not read into the text box) until the end of the text file.
What do you mean 'broken by count'? It splits the 1 string on every occurance of the Carriage Line Return type character and makes an array of strings representing each line. If you don't want the first 4 lines then just start at lines(4) since its a zero based array. If you need to check what they start with use the string function StartsWith like this: lines(4).Startswith("whattocheckforhere"). That will return true or false if that is what the line starts with.
Ok I think Im on the right track, here is what I came up with but textbox doent display text. Trying to get code to write all lines that begin with 4 spaces:
Code:
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
Dim fs As FileStream = New FileStream(Path.Combine(Application.StartupPath, "C:\Program Files\Novalogic\Delta Force Land Warrior\scores.txt"), FileMode.Open)
Dim sr As New BinaryReader(fs)
Dim filedata(fs.Length) As Char
sr.ReadBytes(fs.Length).CopyTo(filedata, 0)
MsgBox(filedata, , "This is the filedata:")
Dim str As String = filedata
'txtFile.Text = str
Do Until Me.Text.StartsWith(" ")
Dim lines() As String
lines = Split(str, ControlChars.NewLine)
Me.Text = lines.GetUpperBound(0)
If Me.Text.StartsWith(" ") = True Then
txtFile.Text = Me.Text
End If
Loop
sr.Close()
fs.Close()
Last edited by Brian Delphino; Sep 22nd, 2002 at 11:42 PM.