vc6.0控制台下写日志文件

来源:互联网 发布:防网络诈骗主题班会 编辑:程序博客网 时间:2024/06/05 16:12

#include <time.h>
#include <fstream>
#include <iostream>
#include <string.h>
#include <windows.h>
using namespace std;

int main(int argc, char* argv[])
{
 RcLogInfo loginfo;
 time_t curTime = time(NULL);
 struct tm *mt = localtime(&curTime);
 const char g_LogRootPath[] = "My_APPLOG";
 char pStrPath[256];
    /* 根据日期组成文件夹名称*/
    int k = CreateDirectory(g_LogRootPath, NULL);
    sprintf(pStrPath, "%s\\%d%02d%02d",g_LogRootPath,
  mt->tm_year + 1900, mt->tm_mon + 1, mt->tm_mday);
 strcat(pStrPath, ".log");
// int n = loginfo.Create_LogFile(pStrPath);
// int k1 = loginfo.IsFileExist(pStrPath);
 char szLogText[256];
 memset(szLogText, 0, 256);
 struct tm *mt1 = localtime(&curTime);
 sprintf(szLogText, "%d-%02d-%02d %02d:%02d:%02d",mt1->tm_year+1900,mt1->tm_mon+1,
  mt1->tm_mday,mt1->tm_hour,mt1->tm_min,mt1->tm_sec);
 
 char Logtext[512] = "护系统的维护1护系统的维护1护系统的维护1";
 int leng = strlen(Logtext); 
 if (leng >=85)
 {
  char Logtext1[512];
  memset(Logtext1,0,sizeof(Logtext1));
  memcpy(Logtext1,Logtext,sizeof(Logtext));  
  sprintf(Logtext, "%s\\L$%d%02d%02d%02d%02d%02d.log",g_LogRootPath,mt1->tm_year+1900,mt1->tm_mon+1,
   mt1->tm_mday,mt1->tm_hour,mt1->tm_min,mt1->tm_sec); 
  FILE *zpFile;
  zpFile = fopen(Logtext,"a+");
  if(NULL == zpFile)
  {
   return -1;
  }
  fwrite(Logtext1, 1,strlen(Logtext1), zpFile);
  fclose(zpFile);
 }
 strcat(szLogText,"\tINFO\t");//类型不能超过20个字节
 strcat(szLogText,Logtext);
 strcat(szLogText,"\n"); 
 FILE *pFile = NULL; 
 pFile = fopen(pStrPath, "a+");
    if(NULL == pFile)
 {
        return -1;
 } 
 fwrite(szLogText, 1,strlen(szLogText), pFile);
 fclose(pFile);
 ifstream infile(pStrPath,ios::in);  
 //infile.open(pStrPath,ios::nocreate);
    if(infile) // 有该文件 
    {
      char buf[120];
   memset(buf,0,sizeof(buf));
   while(infile.getline(buf,120))
   {
   cout<<buf<<endl;
   int num = 0;
   for (int i =0;i<strlen(buf);i++)
   {
    if ('\t'==buf[i])
    {
     num++;
    }
    if (2<=num)
    {
     num = i;
     break;
    }
   }
   num = num +1;
   char zstrInfo[256];
   memset(zstrInfo,0,sizeof(zstrInfo));
   memcpy(zstrInfo,buf+num,sizeof(buf)-num);
   FILE *pFile = NULL; 
   pFile = fopen(zstrInfo, "r");
   if(NULL != pFile)
   {
    cout<<"有++:";
    memset(zstrInfo,0,sizeof(zstrInfo));
    while(fread(zstrInfo,1,sizeof(zstrInfo),pFile))
    {
     cout<<zstrInfo; 
    }
    cout<<endl;
   } 
   }
 }
 else // 没有该文件 
    { 
        cout <<"no such file" << endl; 
    }
 return 0;
}

0 0
原创粉丝点击