58 lines
1.1 KiB
C++
58 lines
1.1 KiB
C++
#include "LogFile.h"
|
|
|
|
|
|
|
|
CLogFile::CLogFile()
|
|
{
|
|
}
|
|
|
|
|
|
CLogFile::~CLogFile()
|
|
{
|
|
}
|
|
|
|
void CLogFile::WriteStrToFile(string strBody) {
|
|
HANDLE hWrite;
|
|
DWORD dwWriteSize;
|
|
char *str = "d:\\log.txt";
|
|
int lenA = lstrlenA(str);
|
|
int lenW;
|
|
BSTR fileName;
|
|
|
|
time_t rawtime;
|
|
struct tm * timeinfo;
|
|
time(&rawtime);
|
|
timeinfo = localtime(&rawtime);
|
|
printf("The current date/time is: %s", asctime(timeinfo));
|
|
char szGetBuffer[512];
|
|
sprintf_s(szGetBuffer, " %s\r\n", asctime(timeinfo));
|
|
strBody += szGetBuffer;
|
|
|
|
lenW = ::MultiByteToWideChar(CP_UTF8, 0, str, lenA, 0, 0);
|
|
if (lenW > 0)
|
|
{
|
|
// Check whether conversion was successful
|
|
fileName = ::SysAllocStringLen(0, lenW);
|
|
::MultiByteToWideChar(CP_UTF8, 0, str, lenA, fileName, lenW);
|
|
}
|
|
else
|
|
{
|
|
// handle the error
|
|
}
|
|
|
|
hWrite = CreateFile(str, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
if (hWrite == INVALID_HANDLE_VALUE)
|
|
{
|
|
printf("open error");
|
|
}
|
|
SetFilePointer(hWrite, 0, 0, FILE_END);//??
|
|
if (!WriteFile(hWrite, strBody.c_str(), strBody.length(), &dwWriteSize, NULL))
|
|
{
|
|
printf("error while writeing");
|
|
}
|
|
|
|
CloseHandle(hWrite);
|
|
// when done, free the BSTR
|
|
::SysFreeString(fileName);
|
|
}
|