C++: Reading an UTF-8 file with Win32 API

thebluetropics 1,046 Reputation points
2022-09-27T04:14:46.687+00:00

I have entering the file IO section of Win32 API. This time, I wanted to read file from.

Here is what I tried so far. This code outputs  ̄テᄄ ̄テᆱ ̄ツᄋ ̄ツᆱ instead of Japanese word ヨルシカ which is weird.
I know this problem, in this code, I didn't read the file as an UTF-8 file, instead I read the file as an ANSI file (256 characters).

So how can I read an UTF-8 file and then put the result in wchar_t array, using built-in Win32 API?

I want the readed character as single UTF-8 character. So each character in the buffer is UTF-8 character, not 256 character.

Windows development | Windows API - Win32
Developer technologies | C++
Developer technologies | C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
{count} votes

Answer accepted by question author
  1. Jeanine Zhang-MSFT 11,356 Reputation points Microsoft External Staff
    2022-09-27T06:39:33.733+00:00

    Hi,@thebluetropics

    As far as I'm concerned you could try to use MultiByteToWideChar function with CP_UTF8.


1 additional answer

Sort by: Most helpful
  1. Guido Franzke 2,191 Reputation points
    2022-09-27T06:08:18.613+00:00

    Hello,
    have a look at this: read-unicode-utf-8-file-into-wstring
    Regards, Guido


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.