Read Unicode UTF-8 file into wstring

前端 未结 6 1757
无人及你
无人及你 2020-11-30 00:26

How can I read a Unicode (UTF-8) file into wstring(s) on the Windows platform?

6条回答
  •  失恋的感觉
    2020-11-30 01:15

    This is a bit raw, but how about reading the file as plain old bytes then cast the byte buffer to wchar_t* ?

    Something like:

    #include 
    #include 
    std::wstring ReadFileIntoWstring(const std::wstring& filepath)
    {
        std::wstring wstr;
        std::ifstream file (filepath.c_str(), std::ios::in|std::ios::binary|std::ios::ate);
        size_t size = (size_t)file.tellg();
        file.seekg (0, std::ios::beg);
        char* buffer = new char [size];
        file.read (buffer, size);
        wstr = (wchar_t*)buffer;
        file.close();
        delete[] buffer;
        return wstr;
    }
    

提交回复
热议问题