Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Reading å, ä, ö from file with StreamReader

Reading å, ä, ö from file with StreamReader

Scheduled Pinned Locked Moved C#
question
12 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    anderslundsgard
    wrote on last edited by
    #1

    Hi. I’m unfourtunately in this case from Sweden which have the letters å, ä, ö. When I read lines in a textfile using StreamReader I got a “?” symbol when I print these letters. Any idea what to do… _____________________________ ...and justice for all APe

    M A S C M 5 Replies Last reply
    0
    • A anderslundsgard

      Hi. I’m unfourtunately in this case from Sweden which have the letters å, ä, ö. When I read lines in a textfile using StreamReader I got a “?” symbol when I print these letters. Any idea what to do… _____________________________ ...and justice for all APe

      M Offline
      M Offline
      mav northwind
      wrote on last edited by
      #2

      You'll have to give the encoding of the file when constructing the StreamReader. Regards, mav

      A 1 Reply Last reply
      0
      • A anderslundsgard

        Hi. I’m unfourtunately in this case from Sweden which have the letters å, ä, ö. When I read lines in a textfile using StreamReader I got a “?” symbol when I print these letters. Any idea what to do… _____________________________ ...and justice for all APe

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        maybe , you need a font file

        A 1 Reply Last reply
        0
        • M mav northwind

          You'll have to give the encoding of the file when constructing the StreamReader. Regards, mav

          A Offline
          A Offline
          anderslundsgard
          wrote on last edited by
          #4

          Any Idea of which Encoding I should use. I've tried the regular once. _____________________________ ...and justice for all APe

          M 1 Reply Last reply
          0
          • A anderslundsgard

            Any Idea of which Encoding I should use. I've tried the regular once. _____________________________ ...and justice for all APe

            M Offline
            M Offline
            mav northwind
            wrote on last edited by
            #5

            Depends on the encoding that was used to create the file. You can try Encoding.Default for a start (which is _not_ the same as not specifying an encoding, strangely enough). Regards, mav

            A 1 Reply Last reply
            0
            • A Anonymous

              maybe , you need a font file

              A Offline
              A Offline
              anderslundsgard
              wrote on last edited by
              #6

              Font file... Sorry I don't understand how I should use that... _____________________________ ...and justice for all APe

              1 Reply Last reply
              0
              • M mav northwind

                Depends on the encoding that was used to create the file. You can try Encoding.Default for a start (which is _not_ the same as not specifying an encoding, strangely enough). Regards, mav

                A Offline
                A Offline
                anderslundsgard
                wrote on last edited by
                #7

                Noppe. I've gone through ASCII, Unicode, Default, snd some more. Can there be something else that is wrong with my language settings of my CPU? I've shecked that an it is "Swedish". _____________________________ ...and justice for all APe

                1 Reply Last reply
                0
                • A anderslundsgard

                  Hi. I’m unfourtunately in this case from Sweden which have the letters å, ä, ö. When I read lines in a textfile using StreamReader I got a “?” symbol when I print these letters. Any idea what to do… _____________________________ ...and justice for all APe

                  S Offline
                  S Offline
                  Sebastian Schneider
                  wrote on last edited by
                  #8

                  May sound stupid, but: Does the font you are using for display have those characters? If you are using a different encoding, you should get wrong characters, not just question-marks. Lets say you have a file with ÄäÖö in it. With a wrong encoding, you should get something like JWpM, since there are different chars there. With a wrong font, you will get like "Box","Box","Box","Box" or ????. Windows sees that there is no character in the font for the keycode given and then prints its "standard"-missing-char-in-font symbol. Try different "standard" fonts (those who come with windows), like "Arial Unicode", "Times", "Verdana", "Arial" ... Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.

                  A 1 Reply Last reply
                  0
                  • S Sebastian Schneider

                    May sound stupid, but: Does the font you are using for display have those characters? If you are using a different encoding, you should get wrong characters, not just question-marks. Lets say you have a file with ÄäÖö in it. With a wrong encoding, you should get something like JWpM, since there are different chars there. With a wrong font, you will get like "Box","Box","Box","Box" or ????. Windows sees that there is no character in the font for the keycode given and then prints its "standard"-missing-char-in-font symbol. Try different "standard" fonts (those who come with windows), like "Arial Unicode", "Times", "Verdana", "Arial" ... Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.

                    A Offline
                    A Offline
                    anderslundsgard
                    wrote on last edited by
                    #9

                    The file is only a simple txt-file written in Notepad. I'm writing the text back to a txt-file and eaven to the Console and error appears there too. I've tried The UTF8Encoding and then the , å, ä, ö doesn't appear at all. _____________________________ ...and justice for all APe

                    1 Reply Last reply
                    0
                    • A anderslundsgard

                      Hi. I’m unfourtunately in this case from Sweden which have the letters å, ä, ö. When I read lines in a textfile using StreamReader I got a “?” symbol when I print these letters. Any idea what to do… _____________________________ ...and justice for all APe

                      C Offline
                      C Offline
                      Carsten Zeumer
                      wrote on last edited by
                      #10

                      Hi, If the file was created on a windows system it is likely that it uses windows encoding. Try the windows-1252 encoding. This works for german pretty well... /cadi 24 hours is not enough

                      1 Reply Last reply
                      0
                      • A anderslundsgard

                        Hi. I’m unfourtunately in this case from Sweden which have the letters å, ä, ö. When I read lines in a textfile using StreamReader I got a “?” symbol when I print these letters. Any idea what to do… _____________________________ ...and justice for all APe

                        M Offline
                        M Offline
                        Mike Dimmick
                        wrote on last edited by
                        #11

                        You need to know which encoding was used for these letters. It will probably be Windows-1252[^]. The default for StreamReader is UTF-8. Check a binary view of the file (you can use the arrow to the right of the Open button in VS, select Open With..., then choose Binary Editor) to see what the values of those characters are. If it's 1252, å should be 0xE5 (229), ä should be 0xE4 (228) and ö should be 0xF6 (246). However, it could conceivably be an old DOS/OEM codepage, in which case it's likely to be either 437[^] or 850[^]. In both of these, å = 0x86 (134), ä = 0x84 (132), and ö = 0x94 (148). If it's something more obscure you may be able to find it at FileFormat.Info[^]. Input the Unicode value of the characters (the hex values I've given for 1252). When you've worked out what code page you're dealing with, use the following:

                        StreamReader sr =
                        new StreamReader(
                        file,
                        System.Text.Encoding.GetEncoding(1252)
                        );

                        Replace 1252 with the correct code page number. Stability. What an interesting concept. -- Chris Maunder

                        A 1 Reply Last reply
                        0
                        • M Mike Dimmick

                          You need to know which encoding was used for these letters. It will probably be Windows-1252[^]. The default for StreamReader is UTF-8. Check a binary view of the file (you can use the arrow to the right of the Open button in VS, select Open With..., then choose Binary Editor) to see what the values of those characters are. If it's 1252, å should be 0xE5 (229), ä should be 0xE4 (228) and ö should be 0xF6 (246). However, it could conceivably be an old DOS/OEM codepage, in which case it's likely to be either 437[^] or 850[^]. In both of these, å = 0x86 (134), ä = 0x84 (132), and ö = 0x94 (148). If it's something more obscure you may be able to find it at FileFormat.Info[^]. Input the Unicode value of the characters (the hex values I've given for 1252). When you've worked out what code page you're dealing with, use the following:

                          StreamReader sr =
                          new StreamReader(
                          file,
                          System.Text.Encoding.GetEncoding(1252)
                          );

                          Replace 1252 with the correct code page number. Stability. What an interesting concept. -- Chris Maunder

                          A Offline
                          A Offline
                          anderslundsgard
                          wrote on last edited by
                          #12

                          Thank You Mike Dimmick and cadi! You made it for me, and my week is saved. I'll bring up a skoal to you tomorrow evening. _____________________________ ...and justice for all APe

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups