How can I read a text file containing Unicode, into a wchar_t pointer using wifstream?

谁都会走 提交于 2020-07-10 04:15:08

问题


I'm trying to read Unicode characters from a text file into a wchar_t pointer array, using wifstream. Here is a code snippet:

locale::global(std::locale("en_US.UTF-8"));
std::wifstream inputFile("gsmCharacterSet.txt", std::ifstream::binary | std::ifstream::ate);
int length = inputFile.tellg();
inputFile.seekg(0,inputFile.beg);
wchar_t *charArray = new wchar_t[length];
inputFile.read(charArray,length);

It's not working. The length returned is 252 which is the correct file size in bytes. However, the array remains empty.

The following condition returns true:

if ( inputFile.peek() == std::wifstream::traits_type::eof() )
    cout << "File is empty";

I'm compiling the program on Linux using g++. What am I doing wrong? Thanks for the help.

来源:https://stackoverflow.com/questions/40844876/how-can-i-read-a-text-file-containing-unicode-into-a-wchar-t-pointer-using-wifs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!