C language > Expert questions

UTF16 reading with fgetwc and fgetws

<< < (2/2)

TimoVJL:
fgetwc() use mbtowc()
crt support to this function is only partial.

--- Code: ---#include <stdio.h>
#include <locale.h>
#include <string.h>
#include <stdlib.h>
#include <wchar.h>
// http://en.cppreference.com/w/c/string/multibyte/mbtowc
// print multibyte string to wide-oriented stdout
// equivalent to wprintf(L"%s\n", ptr);
void print_mb(const char* ptr)
{
    mbtowc(NULL, 0, 0); // reset the conversion state
    const char* end = ptr + strlen(ptr);
    int ret;
    for (wchar_t wc; (ret = mbtowc(&wc, ptr, end-ptr)) > 0; ptr+=ret) {
        wprintf(L"%lc", wc);
    }
    wprintf(L"\n");
}
 
int main(void)
{
    setlocale(LC_ALL, "en_US.utf8");
    // UTF-8 narrow multibyte encoding
    print_mb(u8"z\u00df\u6c34\U0001F34C");"
}
--- End code ---
so better avoid those mb??? functions, if UTF was used.

Navigation

[0] Message Index

[*] Previous page

Go to full version