C++ Read/Write characters bigger than ~50,000
-
27-10-2019 - |
Pergunta
I've got a program that basically writes characters like this
wchar_t c[5] = {static_cast<wchar_t>(x1), static_cast<wchar_t>(x2), static_cast<wchar_t>(x3), static_cast<wchar_t>(x4)};
fileWriter->Write(msclr::interop::marshal_as<String^>(c));
And then later has them input back into the program with just
String^ fileContent = fileReader->ReadToEnd();
My problem is when x1/x2/x3/x4 have an integer value of some large number (I can't be sure which, but I think it's somewhere around when it goes above 50,000) it then reads in the character with a value of 65533.
I have messed about with the StreamReader and StreamWriter encoding values, but I have them set to System::Text::Encoding::Unicode at first and can't see why that wouldn't work...
I'm not even sure if it's writing the symbol incorrectly or reading it... When I open the text file that it creates, they're all unreadable characters for my PC... I'm really at a loss for ideas. Does anyone know what I'm doing wrong here?
Solução
This is probably covered by the Unicode FAQ "What is a surrogate?" It is completely unclear what you are actually trying to accomplish but it sure sounds like you should use a FileStream and not a StreamWriter. Bytes and characters are not interchangeable.