I’m using ccs=encoding (as described in MSDN) to set the encoding to UTF-8 when opening a file with fopen.
When writing to a file it works fine
wchar_t* unicode_text = L"こんにちは";
FILE* f = fopen("C:\\test.txt", "w,ccs=UTF-8");
fwprintf(f, L"%s\n", unicode_text);
fclose(f);
When I open the file in a text editor the unicode shows as it should.
But when trying to read from the created file the UTF-8 encoding is not detected:
wchar_t buffer[1000];
FILE* f = fopen("C:\\test.txt", "r,ccs=UTF-8");
fgetws(buffer, 1000, f);
fclose(f);
MessageBoxW(0, buffer, 0, 0);
This shows “ããã«ã¡ã¯” in the message box.
Why does this happen? Is ccs=UTF-8 only valid when opening files for writing?
The documentation implies that UTF-8 encoding is only available for writing (emphasis mine):
Note that “reading an existing file” is conspicuously absent.