I'm trying to read this file (in this case UTF-8 encoded) and then print it to stdout.
The problem is that I don't know how to detect and tell fgetws what encoding is used in the file. Also, I would like to store everything internally as UTF-32/UCS-4 (wchar_t is 32 bits in GLIBC) in wchar_t variables, and then convert the text to the current locale on output.
This is what I've got so far, but it only prints garbage (my locale: LC_CTYPE=en_US.UTF-8).
Also, I have to compile with std=c99 instead of ansi for some reason, despite that the man pages for wprintf etc say that it conforms to "ISO/ANSI C, UNIX98".Code:#include <errno.h> #include <locale.h> #include <stdio.h> #include <wchar.h> int main() { const char filename[] = "UTF-8-demo.txt"; wchar_t buf[80]; FILE *fp; if (!setlocale(LC_ALL, "")) { fprintf(stderr, "Failed to set the specified locale\n"); return 1; } fp = fopen(filename, "r"); if (!fp) { perror(filename); return 1; } while (fgetws(buf, 80, fp)) wprintf(L"%s", buf); return 0; }



LinkBack URL
About LinkBacks



