CString strSource =L"你好"
CString strfile, str, strCurTime;
HANDLE hFind = NULL, hFile1 = NULL;
WIN32_FIND_DATA FileData;
CFile cfile;
int stringsize;
LPSTR lpstr= NULL;
SYSTEMTIME Systime;
SYSTEMTIME LocalTime, LocalTime1;
memset(&FileData, 0, sizeof(FileData));
strfile=_T("//file//warn.txt");
hFind = FindFirstFile(strfile, &FileData);
if ( hFind == INVALID_HANDLE_VALUE )
{//不存在就创建
hFile1= CreateFile(strfile,GENERIC_WRITE|GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,
CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
CloseHandle(hFile1);
FindFirstFile(strfile, &FileData);
if( !cfile.Open( strfile, CFile::modeReadWrite, NULL ) )
{
MessageBox( L" Open warn File Error1", L"提示信息",MB_ICONINFORMATION );
return;
}
GetLocalTime(&LocalTime);
strCurTime.Format(_T(" 创建文件时间: %4d-%.2d-%.2d %.2d:%.2d"),LocalTime.wYear,LocalTime.wMonth,
LocalTime.wDay,LocalTime.wHour,LocalTime.wMinute);
cfile.SeekToEnd();
stringsize=WideCharToMultiByte(CP_ACP,0,strCurTime,-1,0,0,NULL,NULL);
lpstr=new char[stringsize*sizeof(TCHAR)];
WideCharToMultiByte(CP_ACP,0,strCurTime,-1,lpstr,stringsize,NULL,NULL);
cfile.Write(lpstr, stringsize-1);
delete[] lpstr;
lpstr = NULL;
//换行
cfile.SeekToEnd();
stringsize=WideCharToMultiByte(CP_ACP,0,L"/r/n",-1,0,0,NULL,NULL);
lpstr=new char[stringsize*sizeof(TCHAR)];
WideCharToMultiByte(CP_ACP,0,L"/r/n",-1,lpstr,stringsize,NULL,NULL);
cfile.Write(lpstr, stringsize-1);
delete[] lpstr;
lpstr = NULL;
cfile.Close();
}
if( !cfile.Open( strfile, CFile::modeReadWrite, NULL ) )
{
MessageBox( L" Open warn File Error2", L"提示信息",MB_ICONINFORMATION );
}
else
{
FileTimeToSystemTime(&FileData.ftLastWriteTime,&Systime);
GetLocalTime(&LocalTime1);
if( LocalTime1.wDay != Systime.wDay || LocalTime1.wMonth != Systime.wMonth
|| LocalTime1.wYear != Systime.wYear )
{
strCurTime.Format(_T("%4d-%.2d-%.2d %.2d:%.2d"),LocalTime1.wYear,LocalTime1.wMonth,
LocalTime1.wDay,LocalTime1.wHour,LocalTime1.wMinute);
cfile.SeekToEnd();
stringsize=WideCharToMultiByte(CP_ACP,0,strCurTime,-1,0,0,NULL,NULL);
lpstr=new char[stringsize*sizeof(TCHAR)];
WideCharToMultiByte(CP_ACP,0,strCurTime,-1,lpstr,stringsize,NULL,NULL);
cfile.Write(lpstr, stringsize-1);
delete[] lpstr;
lpstr = NULL;
cfile.SeekToEnd();
stringsize=WideCharToMultiByte(CP_ACP,0,L"/r/n",-1,0,0,NULL,NULL);
lpstr=new char[stringsize*sizeof(TCHAR)];
WideCharToMultiByte(CP_ACP,0,L"/r/n",-1,lpstr,stringsize,NULL,NULL);
cfile.Write(lpstr, stringsize-1);
delete[] lpstr;
lpstr = NULL;
}
strCurTime.Format(_T("%.2d:%.2d "),LocalTime1.wHour,LocalTime1.wMinute);
cfile.SeekToEnd();
stringsize=WideCharToMultiByte(CP_ACP,0,strCurTime,-1,0,0,NULL,NULL);
lpstr=new char[stringsize*sizeof(TCHAR)];
WideCharToMultiByte(CP_ACP,0,strCurTime,-1,lpstr,stringsize,NULL,NULL);
cfile.Write(lpstr, stringsize-1);
delete[] lpstr;
lpstr = NULL;
cfile.SeekToEnd();
stringsize=WideCharToMultiByte(CP_ACP,0,strSource,-1,0,0,NULL,NULL);
lpstr=new char[stringsize*sizeof(TCHAR)];
WideCharToMultiByte(CP_ACP,0,strSource,-1,lpstr,stringsize,NULL,NULL);
cfile.Write(lpstr, stringsize-1);
delete[] lpstr;
lpstr = NULL;
cfile.SeekToEnd();
stringsize=WideCharToMultiByte(CP_ACP,0,L"/r/n",-1,0,0,NULL,NULL);
lpstr=new char[stringsize*sizeof(TCHAR)];
WideCharToMultiByte(CP_ACP,0,L"/r/n",-1,lpstr,stringsize,NULL,NULL);
cfile.Write(lpstr, stringsize-1);
delete[] lpstr;
lpstr = NULL;
cfile.Close();
}
FindClose( hFind );