Issue
Here is my simplified code:
static void WriteToFile(const wchar_t* msg) {
FILE* f = fopen("/sdcard/mytest.txt", "w");
fprintf(f, "%ls\n", msg);
fclose(f);
}
// Somewhere else in the code
const wchar_t* msg = L"Hello world";
WriteToFile(msg);
Using %ls
to format a wchar string seems to work fine in Windows and Ubuntu. However, on Android, it writes only the first character H
in the file.
I even tried to convert wchar to mbs:
char buf[100];
wcstombs(buf, msg, 100);
However, buf
still ends up having just one character H
in it.
I have a feeling that it is happening because wchar is four bytes long on Android. However, I would think the NDK must be aware of this.
How do I fix this? Regards.
Solution
Instead of using wcstombs, etc., use APIs from libiconv (https://www.gnu.org/software/libiconv/). You may have to build libiconv library for Android.
Answered By - Peter
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.