I'm currently studying MFC library CFile class and I have a problem with writing data to a file using Write method. When I pass char array as parameter it works perfectly fine:
char c[] = "Hello!";
int size = sizeof(c) / sizeof(c[0]);
myFile.Write(c, size)
Characters that were written to file:
Hello!
But when I try to pass CString object as an argument:
CString cS("Hello");
myFile.Write(cS, cS.GetLength());
I get:
H e l
I also tried:
CString cS("Hello");
LPWSTR c = cS.GetBuffer();
myFile.Write(c, cS.GetLength());
cS.ReleaseBuffer();
But the output is the same as above. What may be cause that conversion? Does it happen because text is stored in wide characters?
Problem:
The second parameter to CFile::Write is the number of bytes that the function will transfer from the first parameter (the buffer). You're passing cS.GetLength()
, which would rather pass the number of characters in the string, and this is not the same as the number of bytes that the string itself may be composed of.
Solution:
You should change the line that writes the string into file to something like this:
myFile.Write(LPCTSTR(cS), cS.GetLength()*sizeof(TCHAR));
sizeof(TCHAR)
will yield a different number based on if you're building for Unicode or MBCS. This is because TCHAR
is defined as wchar_t
for Unicode builds, and as char
for MBCS builds. As such, multiplying the length of string with the size of TCHAR will always equal the number of bytes the string is composed of regardless of if you're building for Unicode or not.
Other points to note:
You've no reason to call GetBuffer()
and ReleaseBuffer()
here, whatsoever.
This point is not a major, but the CFile::Write
function takes a const void *
as its first argument. So you should rather be casting your CString
to LPCTSTR
(which would automatically evaluate to LPCWSTR
or LPCSTR
based on if you're building with Unicode or MBCS).
One last thing: It's good to wrap string literals with the _T()
macro so that you could compile for Unicode and MBCS alike without having to change your code.
Applying all changes, your whole code would look something like this:
CString cS(_T("Hello"));
myFile.Write(LPCTSTR(cS), cS.GetLength()*sizeof(TCHAR));
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments