PDA

Click to See Complete Forum and Search --> : Using streamreader to open text as binary array? How?


Brian Delphino
Sep 19th, 2002, 10:33 PM
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:




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

Edneeis
Sep 19th, 2002, 11:19 PM
Here is how to read in a textfile from a filestream:

'Open the file to a stream
Dim fs As FileStream = New FileStream(Path.Combine(Application.StartupPath, "test.txt"), FileMode.Open)
Dim sr As New StreamReader(fs)
Dim filedata As String = sr.ReadToEnd()
MsgBox(filedata)
sr.Close()
fs.Close()

Edneeis
Sep 19th, 2002, 11:22 PM
What objects are in the RichTextBox? Do you have pictures or something in there? If not then you can just do this:

Dim fs As FileStream=New FileStream("c:\DoneStats.txt", FileMode.Append)
Dim sw As New StreamWriter(fs)
sw.Write(RichTextBox1.Text)
sw.Close()
fs.Close()

Edneeis
Sep 19th, 2002, 11:27 PM
Although I just tested and that code doesn't seem to solve your problem. Let me try something else.

Edneeis
Sep 19th, 2002, 11:32 PM
Ok this worked:

Dim fs As FileStream = New FileStream(Path.Combine(Application.StartupPath, "test.txt"), FileMode.Open)
Dim sr As New BinaryReader(fs)
'buffer the char to hold the contents of the file
Dim filedata(fs.Length) As Char
'copy byte array to char array to view as a string
sr.ReadBytes(fs.Length).CopyTo(filedata, 0)
MsgBox(filedata)
sr.Close()
fs.Close()

Brian Delphino
Sep 20th, 2002, 08:49 PM
Thanx bro, just got home from work Ill try it and post results!

Thanx!!!

Edneeis
Sep 20th, 2002, 09:20 PM
Also you can convert the Char() into a string if you need to.

Brian Delphino
Sep 21st, 2002, 04:30 AM
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?

PT Exorcist
Sep 21st, 2002, 11:00 AM
the problem isnt in the stream but in the richtextbox...look for that and not for the stream...i heared a friend of me having a similiar issue..

Brian Delphino
Sep 21st, 2002, 08:23 PM
I tried this code with both the RichTextBox and Textbox, I still have the same issue.

PT Exorcist
Sep 21st, 2002, 08:44 PM
hmm then dont know :\

Brian Delphino
Sep 21st, 2002, 09:35 PM
there has got to be a way to do this!!!!!!!

Edneeis
Sep 22nd, 2002, 01:12 AM
I don't know worked fine for me, here is a sample project.

Brian Delphino
Sep 22nd, 2002, 08:18 PM
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:

1 ¤*°§ÇÅR£Ð ?°*¤ 106 28 12
2 =Gunner=Ulle DK 45 12 15
3 Tokugawa Ieyasu 20 5 11
4 10 3 1
5 Lady Domina 9 4 3
6 Savij 5 3 2
7 Porcupine 0 0 0
8 g-lady 2 0 0 0
9 Silver 0 0 4

1 ¤*°§ÇÅR£Ð ?°*¤ 104 39 23
2 Lady Domina 88 38 18
3 music man 52 24 19
4 Savij 49 23 31
5 Porcupine 46 19 11
6 30 14 22
7 Tokugawa Ieyasu 20 9 2
8 thor4 20 9 9
9 blaster 4 1 5
10 slamfist 3 1 2

the numbers are actually more to the right in colums so that I can import in to Excel

Edneeis
Sep 22nd, 2002, 09:45 PM
Once the data is read into the string you can split it by line (controlchars.newline). Here is what I mean:

'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.

Brian Delphino
Sep 22nd, 2002, 10:31 PM
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.

How do distinguish these lines?

Edneeis
Sep 22nd, 2002, 10:48 PM
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.

Brian Delphino
Sep 22nd, 2002, 11:38 PM
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:


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()

Edneeis
Sep 22nd, 2002, 11:52 PM
Try this you have some redundent stuff in there:

'there is no need to use the path.combine, app.startpath bit because you are listing the fullpath
Dim fs As FileStream = New FileStream("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)
sr.Close()
fs.Close()

Dim str As String = filedata
Dim lines() As String
lines = Split(str, ControlChars.NewLine)
Dim ln as string
'the following goes thru each line and test for the start
For Each ln in Lines
If ln.StartsWith(" ") = True Then
'if a match is found APPEND the text to the existing text in the box
txtFile.Text &= String.Concat(ln,ControlChars.NewLine)
End If
Next

Brian Delphino
Sep 23rd, 2002, 01:49 AM
This works fine. Thanx for help!!

Edneeis
Sep 23rd, 2002, 02:39 AM
No problem, watch out if you see a Sgt.Baker in the DFLW games or my friend Captain Barabas

Brian Delphino
Sep 23rd, 2002, 03:26 AM
Dude come by our sniper server, we are usually on page 1 of public games, look for "DP Snipers Stats".

Also check us out at www.doom-patrol.com. You can visit the stats page and see how my stats are posted.

Thanks for your help!

-Doc