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

The encoding is Unicode. I want to write a string like CString to a file and then read it from the file. I am writing a line to a file with the CFile :: Write () method:

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

      

Here is the question: I want to get a CString from a file containing the contents of strSample. How can i do this?

Many thanks!

+2


source to share


3 answers


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

      



+5


source


I think you forgot to include the '\ 0' at the end



strSample.GetLength () + 1

0


source


I would try this as maybe the file is larger than the one being read (final DOS style line). Read does not set the final \ 0, but ReleaseBuffer apparently does if not called with -1.

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

      

0


source







All Articles