创建文件、查找文件、获取lastWriteTime、写文件

 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 );

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值