We help IT Professionals succeed at work.

vb.net - StreamReader.ReadToEnd - IGNORES SPECIAL CHARACTERS

jasww
jasww asked
on
3,554 Views
Last Modified: 2007-12-19
I have a file with odd characters in it.
The following code IGNORES those charcters:

        Dim ssContents As String
        Dim soReader As System.IO.StreamReader
        soReader = New StreamReader("C:\path\to\file.txt")
        ssContents = soReader.ReadToEnd
        soReader.Close()        

My file contains this:
    00000097.5è;99;;;

The ssContents variable, having read in the text, contain this:
   00000097.5;99;;;

NB. No "è" character!


This not that the string cannot hold the char, as I have forced it into it, but that the reader object ignores it.

Please advise.

Easy but urgent - 250pts.
Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Coolio.
What ebncoding should I use?
Ascii gives "?", UTF8 ignores them, UTF7 looks ok.  But will this cause me more problems elsewhere?

Commented:
It depends on your local settings (regional options in Windows). .NET will use them by default.

Author

Commented:
Is there a better way of reading the file in. perhaps a way that allows me to use Latin-1 encoding?

StreamReader is just one way, there must be others...
CERTIFIED EXPERT

Commented:
Hi jasww,
Here is a sample of single char read. I think it might help troubleshoot or fix your problem.

    Public Sub StreamReaderReadINCHARS(ByVal sFileName As String)
        Dim myStreamReader As StreamReader
        Dim myNextInt As Integer
 
        Try
            ' Create a StreamReader using a Shared (static) File class.
            myStreamReader = File.OpenText(sFileName)
   
            ' The Read() method returns an integer.
            myNextInt = myStreamReader.Read()
            ' The Read() method returns '-1' when the end of the
            '   file has been reached
            While myNextInt <> -1
                ' Convert the integer to a unicode Char and add it
                '   to sData var.
                sData += ChrW(myNextInt)      '<------sData is just a global var.
                ' Read the next value from the Stream
                myNextInt = myStreamReader.Read()
                ' Refresh the text box so that the user can see
                '   the characters being added.
                '     txtFileText.Refresh()   '<--- use a textbox if you want to see it.
                ' Sleep for 100 milliseconds, so that the user can
                '   see that the characters are being read one at a
                '   time. Otherwise, they display too quickly.
                System.Threading.Thread.CurrentThread.Sleep(100)
            End While
        Catch exc As Exception
            MsgBox("File could not be opened or read." + vbCrLf + _
                "Please verify that the filename is correct, " + _
                "and that you have read permissions for the desired " + _
                "directory." + vbCrLf + vbCrLf + "Exception: " + exc.Message)
        Finally
            ' Close the object if it has been created.
            If Not myStreamReader Is Nothing Then
                myStreamReader.Close()
            End If
        End Try
    End Sub

Commented:
The streamreader is the best way to do it. Also the fastest.
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
iboutchkine, please read up, that's what already said ;-)

Author

Commented:
Thanks guys.
Rover, you're the early bird.
iboutchkine, I needed the code for Latin-1, thanks
planocz, your sub suffers the same fate as my original code, in that it ignores the funny chars.

Thanks

Dnaiel - jasww
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.