I want to read a short line from UTF-8 file and display it in Windows console.
I succeeded with MultiByteToWideChar Winapi function:
void mbtowchar(const char* input, WCHAR* output) {
int len = MultiByteToWideChar(CP_UTF8, 0, input, -1, NULL, 0);
MultiByteToWideChar(CP_UTF8, 0, input, -1, output, len);
}
void main() {
setlocale(LC_ALL,"");
char in[256];
FILE* file = fopen("data.txt", "r");
fgets(in, 255, file);
fclose(file);
mbtowchar(in, out);
printf("%ls",out);
}
...but I failed with ISO mbsrtowcs function (non-ASCII chars are messed):
void main() {
setlocale(LC_ALL,"");
char in[256];
wchar_t out[256];
FILE* file = fopen("data.txt", "r");
fgets(in, 255, file);
fclose(file);
const char* p = in;
mbstate_t mbs = 0;
mbsrtowcs(out, &p, 255, &mbs);
printf("%ls",out);
}
Do I do something wrong with mbsrtowcs or is there some important difference between these two functions? Is it possible to reliably print UTF-8 in windows console using ISO functions? (Assuming matching console font is installed.)
Notes: I use MinGW gcc compiler. C++ is the last resort solution for me, I'd like to stay with C.