繁体   English   中英

如何使用Unicode中的CFile :: Read()从文件中获取CString对象?

[英]How to get a CString object from a file with CFile::Read() in Unicode?

字符集是Unicode。 我想将CString类型的字符串写入文件,然后再从文件中读出。 我使用CFile :: Write()方法将字符串写入文件:

int nLen = strSample.GetLength()*sizeof(TCHAR);
file.Write(strSample.GetBuffer(), nLen);

这里的问题是:我想从包含strSample内容的文件中获取CString。 我该怎么做?

非常感谢你!

UINT nBytes = (UINT)file.GetLength();
int nChars = nBytes / sizeof(TCHAR);
nBytes = file.Read(strSample.GetBuffer(nChars), nBytes);
strSample.ReleaseBuffer(nChars);

我会尝试这样做,因为它可能是文件比读取的文件大(DOS样式的结束行)。 如果未使用-1调用,则Read不会设置最终\\ 0,但ReleaseBuffer显然可以。

UINT nBytes = (UINT)file.GetLength();
UINT nBytesRead = file.Read(strSample.GetBuffer(nBytes+1), nBytes);
strSample.ReleaseBuffer(nBytesRead);

我认为您忘了在最后加上'\\ 0'

strSample.GetLength()+ 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM