Can't read a UTF8 encoded file using fopen(file, "r,ccs=UTF-8")
Solution 1
The documentation implies that UTF-8 encoding is only available for writing (emphasis mine):
In Visual C++ 2005, fopen supports Unicode file streams. A flag specifying the desired encoding may be passed to fopen when opening a new file or overwriting an existing file, like this:
Note that "reading an existing file" is conspicuously absent.
Solution 2
try this
#include <locale.h>
setlocale(LC_ALL, "Japanese");
sashoalm
Updated on June 04, 2022Comments
-
sashoalm almost 2 years
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?