如何使用 Unicode 中的 CFile::Read() 从文件中获取 CString 对象?
字符集是 Unicode。我想将一个CString类型的字符串写入一个文件,然后再从文件中读出。 我使用 CFile::Write() 方法将字符串写入文件:
int nLen = strSample.GetLength()*sizeof(TCHAR);
file.Write(strSample.GetBuffer(), nLen);
问题是:我想从包含 strSample 内容的文件中获取 CString。我该怎么做呢?
非常感谢!
The charset is Unicode. I want to write a string of CString type into a file, and then read it out from the file afterwards.
I write the string into a file with CFile::Write() method:
int nLen = strSample.GetLength()*sizeof(TCHAR);
file.Write(strSample.GetBuffer(), nLen);
Here is the question: I want to obtain the CString from the file containing the content of strSample. How can I do it?
Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
末尾添加 '\0'
我认为您忘记在strSample.GetLength() + 1
I think you forgot to include the '\0' at the end
strSample.GetLength() + 1
我会尝试这个,因为文件可能比读取的文件大(DOS 风格的结束行)。 Read 不会设置最终的 \0,但 ReleaseBuffer 显然会设置,如果不使用 -1 调用的话。
I would try this, since it can be that the file is larger than what is read (DOS style end line). Read does not set the final \0, but ReleaseBuffer apparently does, if not called with -1.