Results 1 to 22 of 22

Thread: Using streamreader to open text as binary array? How?

  1. #1

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    52

    Question 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

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Here is how to read in a textfile from a filestream:
    VB Code:
    1. 'Open the file to a stream
    2.         Dim fs As FileStream = New FileStream(Path.Combine(Application.StartupPath, "test.txt"), FileMode.Open)
    3.         Dim sr As New StreamReader(fs)
    4.         Dim filedata As String = sr.ReadToEnd()
    5.         MsgBox(filedata)
    6.         sr.Close()
    7.         fs.Close()
    Last edited by Edneeis; Sep 19th, 2002 at 11:23 PM.

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    What objects are in the RichTextBox? Do you have pictures or something in there? If not then you can just do this:
    VB Code:
    1. Dim fs As FileStream=New FileStream("c:\DoneStats.txt", FileMode.Append)
    2.         Dim sw As New StreamWriter(fs)
    3.         sw.Write(RichTextBox1.Text)
    4.         sw.Close()
    5.         fs.Close()

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Although I just tested and that code doesn't seem to solve your problem. Let me try something else.

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Ok this worked:
    VB Code:
    1. Dim fs As FileStream = New FileStream(Path.Combine(Application.StartupPath, "test.txt"), FileMode.Open)
    2.         Dim sr As New BinaryReader(fs)
    3.         'buffer the char to hold the contents of the file
    4.         Dim filedata(fs.Length) As Char
    5.         'copy byte array to char array to view as a string
    6.         sr.ReadBytes(fs.Length).CopyTo(filedata, 0)
    7.         MsgBox(filedata)
    8.         sr.Close()
    9.         fs.Close()

  6. #6

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    52
    Thanx bro, just got home from work Ill try it and post results!

    Thanx!!!

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Also you can convert the Char() into a string if you need to.

  8. #8

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    52
    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?

  9. #9
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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..

  10. #10

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    52
    I tried this code with both the RichTextBox and Textbox, I still have the same issue.

  11. #11
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmm then dont know :\

  12. #12

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    52
    there has got to be a way to do this!!!!!!!

  13. #13
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I don't know worked fine for me, here is a sample project.

  14. #14

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    52
    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
    Attached Files Attached Files

  15. #15
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Once the data is read into the string you can split it by line (controlchars.newline). Here is what I mean:
    VB Code:
    1. 'previous code goes here
    2.  
    3.         Dim lines() As String
    4.         lines = Split(str, ControlChars.NewLine)
    5.         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.

  16. #16

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    52

    Question

    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?

  17. #17
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  18. #18

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    52
    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.

  19. #19
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Try this you have some redundent stuff in there:
    VB Code:
    1. 'there is no need to use the path.combine, app.startpath bit because you are listing the fullpath
    2.         Dim fs As FileStream = New FileStream("C:\Program Files\Novalogic\Delta Force Land Warrior\scores.txt", FileMode.Open)
    3.         Dim sr As New BinaryReader(fs)
    4.         Dim filedata(fs.Length) As Char
    5.         sr.ReadBytes(fs.Length).CopyTo(filedata, 0)
    6.         sr.Close()
    7.         fs.Close()
    8.  
    9.         Dim str As String = filedata
    10.         Dim lines() As String
    11.         lines = Split(str, ControlChars.NewLine)
    12.         Dim ln as string
    13.         'the following goes thru each line and test for the start
    14.         For Each ln in Lines
    15.         If ln.StartsWith("    ") = True Then
    16.             'if a match is found APPEND the text to the existing text in the box
    17.             txtFile.Text  &= String.Concat(ln,ControlChars.NewLine)
    18.         End If
    19.         Next
    Last edited by Edneeis; Sep 22nd, 2002 at 11:59 PM.

  20. #20

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    52
    This works fine. Thanx for help!!

  21. #21
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    No problem, watch out if you see a Sgt.Baker in the DFLW games or my friend Captain Barabas

  22. #22

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    52
    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
    Last edited by Brian Delphino; Sep 23rd, 2002 at 03:34 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width