quarta-feira, 24 de abril de 2019

read from stdin and printing to stdout UTF-8 string in C

Adapted (from IBM) example to read UTF-8 string from stdin and printing.

#include <errno.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <wchar.h> 
#include <locale.h> 
int main(void)
{
   char *locale;
   locale = setlocale(LC_ALL, "");
   wint_t  wc;

   errno = 0;
   while (WEOF != (wc = getwchar()))
      printf("wc = %lc\n", wc);

   if (EILSEQ == errno) {
      printf("An invalid wide character was encountered.\n");
      exit(1);
   }
   return 0;
} 

Some usage examples:

$ echo "ção" | ./wtst 
wc = ç
wc = ã
wc = o
wc = 

$ echo "你好" | ./wtst
wc = 你
wc = 好
wc = 

$ echo "Привет" | ./wtst
wc = П
wc = р
wc = и
wc = в
wc = е
wc = т
wc =