问题
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