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!
MultiByteToWideChar) to read it as a sequence of unicode characters.resource.txtis in fact UTF-16 encoded. Since you come to the conclusion that "neither one works", it would help if you explained what does not work (what's the expected result and actual result?).resource.txtfile is encoded in UTF-8, so the snippets above give me a string of arbitrary characters. The result I am looking for is to get awstringof the contents of the file and store that in awstringstream.