TextStream - reading special characters

I am trying to get TextStream to read a log file (text) with a special character in it.  The special character is the hex null (00).  A typical textreader (wordpad) represents it as a square.  This character is randomly placed throughout the log file.  The problem is whenever TextStream reads this characters it interprets it as a "endofstream" and stops reading.  I am not concerned about the character it can me replaced with a space.  I have tried both the ReadAll and the ReadLine but all fails.  Is there anyway around this   Is there some other way besides the "textstream" to read a text file as a string   Is there a way to filter the file first and delete the character   Help!

Answer this question

TextStream - reading special characters

  • Viktor_BVP

     

    Works like a charm!!!!  Thanks for  the help!!


  • James L VanDusen

    Maybe something like this would work for you:  

        Private Sub ReplaceNulls(ByVal bytes() As Byte)
            For byteNo As Integer = 0 To bytes.Length - 1
                ' Replace all NULLs with spaces...
                If bytes(byteNo) = 0 Then
                    bytes(byteNo) = Microsoft.VisualBasic.Asc(" "c)
                End If
            Next
        End Sub

        Sub Main()
            Dim bytes() As Byte
            Using fileToRead As New System.IO.FileStream("FileWithFunkyNulls.txt", IO.FileMode.Open)
                Using reader As New System.IO.BinaryReader(fileToRead)
                    ' Read all data into a temporary buffer so we can replace NULLs with
                    ' spaces... This may not be feasible if the logfile is very big...
                    bytes = reader.ReadBytes(fileToRead.Length)
                    ReplaceNulls(bytes)
                End Using
            End Using

            ' Get the string from the ASCII encoded byte buffer now that we have removed
            ' all the NULLs from the buffer
            Dim resultingString As String = System.Text.Encoding.ASCII.GetString(bytes)

            Console.WriteLine(resultingString)
        End Sub

    Best regards,
    Johan Stenberg



  • ha ha

    Hrm  compiler warnings are your friend. ReplaceNulls shouldn't be a function, it should be a Sub, and the compiler is absolutely correct about warning me that I'm not returning anything.

    I'm sorry for any confusion I may have caused.

    Best regards,
    Johan Stenberg



  • Mage

    How do you open the file Which character encoding is the file using ASCII UTF8 Are you sure you open the file with the appropriate encoding

    I just tried a little ASCII encoded file with a couple of NULL (0x00) characters in them and it works as expected (the NULLs get removed since they aren't valid ASCII code points)

    What you could do is open the file as a binary file (techincally it isn't really a pure text file since it contains NULL characters), read the file as a stream of bytes, replace all instances of 0x00 with a valid code point (i.e. 0x20 - space - for ASCII) and then use the appropriate System.Encoding.GetString(byte()) method to get the string... 

    For info on how to read a binary file, see http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpguide/html/cpconReadingWritingToNewlyCreatedDataFile.asp

    Best regards,
    Johan Stenberg



  • applechen

    Mr Stenberg,

      Thanks so much for the help.  I am glad to hear there is a way around my problem.  I understand your solution however I can't make it work.  I am going to have to ask you to spoon feel me the answer.

      I created a binary file (test.txt) and inserted the null value.  I refered to the help file you sent me and did some cut and pasting.  The only output I received was a series of numbers ...not hex..not anything I recognize. 

    You said that you wrote some code that work fine with the null value.  Could you post it And by the way, the file uses ASCII encoding and I use Wordpad to open it

    Thanks again for your help!

    This following is the code I ran:

     Public Class Form1
        Private Const FILE_NAME As String = "c:\Test.txt"

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim fs = New System.IO.FileStream(FILE_NAME, System.IO.FileMode.Open, System.IO.FileAccess.Read)
            Dim r As New System.IO.BinaryReader(fs)
            Dim i As Integer
            For i = 0 To 10
                Console.WriteLine(r.ReadInt32())
            Next i
        End Sub
    End Class


  • NormLane

    Mr Stenberg, thank you so much for your time and help but when I ran your code I received an error.  If it is something I am doing wrong please let me know.  Just to let you know, I renamed my test file to "FileWithFunkyNulls" (cool name by the way).  This is the error I received:

    Warning 1 Function 'ReplaceNulls' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.  \Form1.vb 9 5 WindowsApplication2

    Again, Thanks for your time!!!


     


  • TextStream - reading special characters