As we know wchar_t
is 2Bytes on windows but 4bytes on macOS/Linux.
I am trying to read a file that has a Unicode string but that string has read incorrectly (unknown symbols).
basic_ifstream<wchar_t> file("/Documents/file.txt", ios_base::ate); // or wifstream
if(!file.is_open()){
cout << "Cannot open the file." << endl;
}
streamsize size = file.tellg();
file.seekg(0);
wstring str (size, 0); // or (size / 4, 0)
file.read(reinterpret_cast<wchar_t*>(&str[0]) , size);
file.close();
When debugging that code to see whether the string has read correctly, I found the string is being read incorrectly (unknown symbols).
What is the correct way to read a Unicode file content into wchar_t
?