-
Text File Writing
Want to write a text file to save log data during app runtime. Grabbed this code from MSDN itself, and it doesnt compile.
HANDLE hFile = CreateFile(_T("C:\\TEMP\\MyFile.txt"),
GENERIC_WRITE, FILE_SHARE_READ,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
AfxMessageBox(_T("Couldn't create the file!"));
else
{
CFile myFile(hFile);
static const char sz[] = "Hockey is best!";
myFile.Write(sz, lstrlen(sz));
myFile.Close();
}
This is the error
'lstrlenW' : cannot convert parameter 1 from 'const char [16]' to 'LPCWSTR'
What am I doing wrong?
-
Re: Text File Writing
Your project is set for Unicode, and the code you show is not. There's a setting in the project properties that lets you change that.
Here's the offending line:
Code:
static const char sz[] = "Hockey is best!";
You could change it to
Code:
static const wchar_t sz[] = _T("Hockey is best!");
Hope that helps.
-
Re: Text File Writing
Thanks - no more error during compilation, but only getting garbage characters in the text file...How do I fix that?
-
Re: Text File Writing
How did you test your file to conclude it contains "garbage"?