MFC从文件中读取unicode字符串到字符串中
我有一个包含很长的 unicode 字符串的文件。 我想将文件中的整个 unicode 字符串读入字符串(或 CString)。 我怎样才能做到这一点? 谢谢
I have a file that contains a very long unicode string.
I want to read the whole unicode string from the file into string (or CString).
How can I do that?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用MFC的CFile打开文件
调用 CFile::GetStatus 获取文件大小 http:// msdn.microsoft.com/en-us/library/fa0hc0ht%28VS.80%29.aspx
分配获取的文件大小的 BYTE 数组并使用 CFile::Read 获取它
http://msdn.microsoft.com/en -us/library/hwbccf8z%28v=VS.80%29.aspx
现在有一个 BOM 问题需要处理。检查缓冲区开头是否存在 BOM 字符。由于您没有指定文件的 unicode 格式,因此您需要参考 http://en。 wikipedia.org/wiki/Byte_order_mark 来弄清楚它们是什么。在继续之前删除 BOM 字符。
现在您可以尝试将原始缓冲区放入 CString 构造函数中。大多数时候这都是有效的。
Use MFC's CFile to open the file
call CFile::GetStatus to acquire file size http://msdn.microsoft.com/en-us/library/fa0hc0ht%28VS.80%29.aspx
allocate a BYTE array of the acquired file size and use CFile::Read to get it
http://msdn.microsoft.com/en-us/library/hwbccf8z%28v=VS.80%29.aspx
Now there's a BOM issue you need to take care of. Check if the BOM chars exists in the beginning of your buffer. Since you did not specify what unicode format is your file, you'll need to refer to http://en.wikipedia.org/wiki/Byte_order_mark to figure out what they are. Remove BOM chars before going on.
Now you can try to throw the raw buffer into CString constructor. Most of the time this works.