Besides using the correct length what is written to file depends also if your project is Unicode or not.
The correct solution to write the
CString
content "as it is" is:
fwrite(data.GetString(), sizeof(TCHAR), data.GetLength(), stream);
With Unicode builds, the
CString
content is stored internally as
wchar_t
which has a size of two bytes. With non-Unicode builds, it is stored as
char
. The
TCHAR
type is the corresponding character type according to the Unicode setting. Because
CString::GetLength()
returns the number of characters, the size has to be multiplied with the size of a character.
If you have a Unicode build but want to write to an ANSI file, you have to convert the string first to ANSI or use an ANSI string:
CStringA dataA = "[ERROR]: 2018-03-21 06:51:02 GMT";
fwrite(dataA.GetString(), 1, dataA.GetLength(), stream);
CString dataAny = _T("[ERROR]: 2018-03-21 06:51:02 GMT");
CStringA dataA(dataAny);
fwrite(dataA.GetString(), 1, dataA.GetLength(), stream);
Note the usage of
CStringA
which is the
char
version.