C++读/写大于 ~50,000 个字符
我有一个程序,基本上写这样的字符
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));
然后将它们输入回程序中
String^ fileContent = fileReader->ReadToEnd();
我的问题是当 x1/x2/x3/x4 具有某个大数字的整数值时(我不能确定(但我认为当它超过 50,000 时)它会读入值为 65533 的字符。
我搞乱了 StreamReader 和 StreamWriter 编码值,但我将它们设置为 System::Text::首先编码::Unicode,不明白为什么这不起作用...
我什至不确定它是否错误地写入了符号或读取了它...当我打开它创建的文本文件时,它们'对于我的电脑来说,这些都是无法读取的字符...我真的不知道该怎么办。有谁知道我在这里做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Unicode 常见问题解答“什么是代理项?”可能涵盖了这一点。完全不清楚你实际上想要完成什么,但听起来你应该使用 FileStream 而不是 StreamWriter。字节和字符不可互换。
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.