Streamreader problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jack Russell

    #1

    Streamreader problem

    I have the following code in VB.net 2003

    Dim s As String
    Dim sr As StreamReader = New StreamReader("t est.txt", New
    UTF7Encoding)
    s = sr.ReadLine
    Debug.Write(s)

    If the string read is 1+.03 it converts it to 1.3

    If I remove the UTF7encoding option it does not.

    Why should the encoding cause a text string to do arithmetic work? and
    can I prevent it.

    Thanks

    Jack Russell
  • Ryan S. Thiele

    #2
    Re: Streamreader problem

    I think it's in the UTF encoding.
    The '+' may not be a valid charactor, so it ignores it.
    Can you avoid UTF7? UTF8 seems to work fine.

    --
    Thiele Enterprises - The Power Is In Your Hands Now!
    "Jack Russell" <jackr@norubbis h.tpg.com.auwro te in message
    news:OXEO%23N88 GHA.3552@TK2MSF TNGP03.phx.gbl. ..
    >I have the following code in VB.net 2003
    >
    Dim s As String
    Dim sr As StreamReader = New StreamReader("t est.txt", New
    UTF7Encoding)
    s = sr.ReadLine
    Debug.Write(s)
    >
    If the string read is 1+.03 it converts it to 1.3
    >
    If I remove the UTF7encoding option it does not.
    >
    Why should the encoding cause a text string to do arithmetic work? and can
    I prevent it.
    >
    Thanks
    >
    Jack Russell

    Comment

    • Jack Russell

      #3
      Re: Streamreader problem

      Ryan S. Thiele wrote:
      I think it's in the UTF encoding.
      The '+' may not be a valid charactor, so it ignores it.
      Can you avoid UTF7? UTF8 seems to work fine.
      >
      You are right, I just discovered that UTF7 regards + as an escape character.

      I have files written by the old Fileopen, printline,filec lose method

      I use streamreader to read these as it seems much faster than openfile,
      lineinput.

      It all works fine until I try to read files containing French such as

      äüöéèà

      The only way I can find to read these is with UTF7, everything else
      ignores them.

      Any ideas gratefully received.

      Jack

      Comment

      • Ryan S. Thiele

        #4
        Re: Streamreader problem

        Try Binary:

        Dim sr As New BinaryReader(Fi le.OpenRead("te st.txt"))
        Do While sr.PeekChar >= 0
        Console.Write(C hr(sr.ReadByte) )
        Loop
        sr.Close()

        Notice I used chr code to convert bye to character.


        --
        Thiele Enterprises - The Power Is In Your Hands Now!


        --
        "Jack Russell" <jackr@norubbis h.tpg.com.auwro te in message
        news:OJxCu9$8GH A.5012@TK2MSFTN GP06.phx.gbl...
        Ryan S. Thiele wrote:
        I think it's in the UTF encoding.
        The '+' may not be a valid charactor, so it ignores it.
        Can you avoid UTF7? UTF8 seems to work fine.
        >
        You are right, I just discovered that UTF7 regards + as an escape character.

        I have files written by the old Fileopen, printline,filec lose method

        I use streamreader to read these as it seems much faster than openfile,
        lineinput.

        It all works fine until I try to read files containing French such as

        äüöéèà

        The only way I can find to read these is with UTF7, everything else
        ignores them.

        Any ideas gratefully received.

        Jack


        Comment

        • Jack Russell

          #5
          Re: Streamreader problem

          Ryan S. Thiele wrote:
          Try Binary:
          >
          Dim sr As New BinaryReader(Fi le.OpenRead("te st.txt"))
          Do While sr.PeekChar >= 0
          Console.Write(C hr(sr.ReadByte) )
          Loop
          sr.Close()
          >
          Notice I used chr code to convert bye to character.
          >
          >
          Well that works thanks but with the large files that I am reading it is
          horrendously slow which is why I went to streamreader in the first
          place!! Any other ideas?

          Comment

          • Chris Dunaway

            #6
            Re: Streamreader problem

            Jack Russell wrote:
            Ryan S. Thiele wrote:
            >
            Try Binary:

            Dim sr As New BinaryReader(Fi le.OpenRead("te st.txt"))
            Do While sr.PeekChar >= 0
            Console.Write(C hr(sr.ReadByte) )
            Loop
            sr.Close()

            Notice I used chr code to convert bye to character.
            Well that works thanks but with the large files that I am reading it is
            horrendously slow which is why I went to streamreader in the first
            place!! Any other ideas?
            Have you tried Encoding.Defaul t instead of UTF7? What encoding was
            used to write the file?

            Comment

            • Ryan S. Thiele

              #7
              Re: Streamreader problem

              Are you editing the text? Use a richtext box...

              Me.RichTextBox1 .LoadFile("Text .exe", RichTextBoxStre amType.PlainTex t)


              --
              Thiele Enterprises - The Power Is In Your Hands Now!

              --
              "Jack Russell" <jackr@norubbis h.tpg.com.auwro te in message
              news:edZCp7A9GH A.3384@TK2MSFTN GP05.phx.gbl...
              Ryan S. Thiele wrote:
              Try Binary:
              >
              Dim sr As New BinaryReader(Fi le.OpenRead("te st.txt"))
              Do While sr.PeekChar >= 0
              Console.Write(C hr(sr.ReadByte) )
              Loop
              sr.Close()
              >
              Notice I used chr code to convert bye to character.
              >
              >
              Well that works thanks but with the large files that I am reading it is
              horrendously slow which is why I went to streamreader in the first
              place!! Any other ideas?


              Comment

              • Jack Russell

                #8
                Re: Streamreader problem

                Chris Dunaway wrote:
                Jack Russell wrote:
                >
                >>Ryan S. Thiele wrote:
                >>
                >>
                >>>Try Binary:
                >>>
                >>>Dim sr As New BinaryReader(Fi le.OpenRead("te st.txt"))
                >>>Do While sr.PeekChar >= 0
                >> Console.Write(C hr(sr.ReadByte) )
                >>>Loop
                >>>sr.Close()
                >>>
                >>>Notice I used chr code to convert bye to character.
                >>>
                >>>
                >>
                >>Well that works thanks but with the large files that I am reading it is
                >>horrendousl y slow which is why I went to streamreader in the first
                >>place!! Any other ideas?
                >
                >
                Have you tried Encoding.Defaul t instead of UTF7? What encoding was
                used to write the file?
                >
                I do not know what encoding was used, it was written using the "old" VB
                method of file open, printline, file close. I have not been able to find
                out what encoding that uses. Just to recap my problem the file can
                contain foregin characters such as äüöéèà. If I do not specify any
                encoding when I open the streamreader these get lost. If I specify UTF7
                they are found but UTF7 treats + as an escape character. I will try
                default but ...

                Comment

                • Andrew Morton

                  #9
                  Re: Streamreader problem

                  Jack Russell wrote:
                  Chris Dunaway wrote:
                  >Have you tried Encoding.Defaul t instead of UTF7? What encoding was
                  >used to write the file?
                  >>
                  I do not know what encoding was used, it was written using the "old"
                  VB method of file open, printline, file close. I have not been able
                  to find out what encoding that uses.
                  I suspect you'll find the bits you need in the help at the
                  System.Text.ASC IIEncoding section, and something to do with Windows-1252 or
                  ISO 8859-1.

                  If you look at your file with a hex editor, do the accented characters
                  correspond to the ones in this table:-


                  ?

                  Also see http://en.wikipedia.org/wiki/ISO_8859-1.

                  HTH

                  Andrew


                  Comment

                  Working...