2

Line:

IList<string> text = await FileIO.ReadLinesAsync(file);

causes exception No mapping for the Unicode character exists in the target multi-byte code page

When I remove chars like ąśźćóż from my file it runs ok, but the problem is that I can't guarantee that those chars won't happen in future.

I tried changing the encoding in advanced save options but it is already

Unicode (UTF-8 with signature) - Codepage 65001

I have a hard time trying to figure this one out.

user3212350
  • 401
  • 1
  • 6
  • 18
  • 1
    Please provide small sample file inline (like 10-20 bytes in HEX) that shows the problem. Otherwise not possible to suggest anything. – Alexei Levenkov Jun 14 '14 at 22:27
  • http://i.imgur.com/vWyXj2O.png Do you mean this? – user3212350 Jun 14 '14 at 23:12
  • sort of. "Inline" as if "edit post and put data as *text*". Your sample file *does not* have BOM (despite you claiming that you've saved it with "Utf8 with signature"), it possible uses some other encoding (probably not Utf8). To fix specify encoding explicitly when reading file or resave file with as Utf8 with BOM. – Alexei Levenkov Jun 14 '14 at 23:22
  • I specified, that I want to use UTF8 encoding explicitly but it doesn't change a thing. Although you are right, when I save file as UTF8 it works alright, but I can't do that, since I can't control what will be given to a program. What gets me is that, when I use normal File with console app it works like a charm, but with FileIO and store apps it fails – user3212350 Jun 15 '14 at 08:42

2 Answers2

0

Make FileIO.ReadLinesAsync use a matching encoding. I don't know what you custom class does but according to the error message it does not use any Unicode encoding.

usr
  • 168,620
  • 35
  • 240
  • 369
  • My class does nothing more than reading from file and saving its lines to a List. I did try to force UTF-8 encoding but it doesn't help. http://pastebin.com/Ph6GKa1K Here is the whole thing – user3212350 Jun 14 '14 at 22:31
  • I'd need to see `FileIO.ReadLinesAsync`. – usr Jun 14 '14 at 22:41
  • http://msdn.microsoft.com/en-us/library/windows/apps/hh701475.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1 – user3212350 Jun 14 '14 at 22:47
  • 1
    I don't see that you specified the encoding. Try this overload: http://msdn.microsoft.com/en-us/library/windows/apps/hh701479.aspx and use the right encoding which would be UTF-8 if we can trust your text editor. – usr Jun 14 '14 at 23:02
  • I did that with UTF-8 and Utf16BE – user3212350 Jun 14 '14 at 23:05
  • 1
    I'd like to see that. You never know what you'll find. Use UTF-8 with BOM header. I don't know how to do that with WinRT but it sure is possible.; Next test: Make sure that using UTF8 and UTF16 result in different strings just to make sure that the encoding "took". – usr Jun 14 '14 at 23:22
0

I think those characters ąśźćóż are UTF-16 encoded.So, it's better to use UTF-16. Use the overload ReadLinesAsync(IStorageFile, UnicodeEncoding) and set UnicodeEncdoing parameter to UnicodeEncoding.Utf16BE

From MSDN :

This method uses the character encoding of the specified file. If you want to specify different encoding, call ReadLinesAsync(IStorageFile, UnicodeEncoding) instead.

Jalal Mostafa
  • 984
  • 10
  • 23
  • it's a little bit confusing, I hate encoding problems. I have seen [this](http://stackoverflow.com/questions/18206590/winrt-no-mapping-for-the-unicode-character-exists-in-the-target-multi-byte-code) on StackOverFlow. I hope the solution suggested there can solve the problem – Jalal Mostafa Jun 14 '14 at 23:13