Reputation: 160
This a follow up of the question asked and answered here. I want to use a text file as a resource and then load it as a stringstream
so that I can parse it.
The following code shows what I currently have:
std::string filename("resource.txt");
HRSRC hrsrc = FindResource(GetModuleHandle(NULL), filename.c_str(), RT_RCDATA);
HGLOBAL res = LoadResource(GetModuleHandle(NULL), hrsrc);
LPBYTE data = (LPBYTE)LockResource(res);
std::stringstream stream((LPSTR)data);
However, I am unsure of how to extend this to read a unicode text file using a wstringstream
. The naive approach yields unreadable characters:
...
LPBYTE data = (LPBYTE)LockResource(res);
std::wstringstream wstream((LPWSTR)data);
Since LPBYTE
is nothing more than a CHAR*
, it is no surprise that this doesn't work, but naively converting the resource to a WCHAR*
(LPWSTR
) does not work either:
...
LPWSTR data = (LPWSTR)LockResource(res);
std::wstringstream wstream(data);
I am guessing this is because a WCHAR
is 16-bit instead of 8-bit like a CHAR
, but I'm not sure how to work around this.
Thanks for any help!
Upvotes: 1
Views: 1162
Reputation: 612954
Your comment supplies the key missing detail. The file that you compiled into a resource is encoded as UTF-8
. So the obvious options are:
MultiByteToWideChar
to convert to UTF-16. Which you can then put into a wstring
.Upvotes: 2