欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

文件读写解析随写

程序员文章站 2022-06-01 23:51:29
...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

//#define LAST_I_INDEX  "last_i_index="
typedef unsigned long dword;

typedef struct{
        int last_i_index;
        int last_r_index;
        int current_i_index;
        int current_r_index;
        int is_i_full;
        int is_r_full;
}log_record_t;
void read_from_seq(log_record_t *log_record)
{
      FILE *fd_read;
      char line[81];
      char *p;
      if(NULL == log_record) return;
      fd_read = fopen("c:\\CBYPRB.SEQ","r");
      if(fd_read)
      {
          while(NULL != fgets(line,80,fd_read)) 
          {
              if(p = strstr(line,"last_i_index="))
                 log_record->last_i_index = atoi(p+strlen("last_i_index=")); 
              if(p = strstr(line,"last_r_index="))
                 log_record->last_r_index = atoi(p+strlen("last_r_index=")); 
              if(p = strstr(line,"current_i_index="))
                 log_record->current_i_index = atoi(p+strlen("current_i_index=")); 
              if(p = strstr(line,"current_r_index="))
                 log_record->current_r_index = atoi(p+strlen("current_r_index=")); 
              if(p = strstr(line,"is_i_full="))
                 log_record->is_i_full = atoi(p+strlen("is_i_full=")) >1 ? 0 : atoi(p+strlen("is_i_full=")); 
              if(p = strstr(line,"is_r_full="))
                 log_record->is_r_full = atoi(p+strlen("is_r_full="))>1 ? 0 : atoi(p+strlen("is_r_full=")); 
                
          }    
          fclose(fd_read);      
      }
     
}
void read_from_log_head(int log_index)
{
      FILE *fd_read;
      char line[81];
      char logname[20];
      char *p;
      char start_time[20];
      char  end_time[20];
      int count = 0;
      bool x = false;
      bool y = false;
      int res = 1;
      

      sprintf(logname,"c:\\test%d.log",log_index);
      fd_read = fopen(logname,"r");
      if(fd_read = fopen(logname,"r"))
          {
               while(NULL != fgets(line,80,fd_read) && count <3) 
               {
                   if(p = strstr(line,"the start date and time : "))
                   {
                        x=true;
                       strncpy(start_time,p+strlen("the start date and time : "),19);
                       start_time[19]='\0';
                   }
                   if(p = strstr(line,"the end date and   time : "))
                   {
                        y= true;
                        strncpy(end_time,p+strlen("the end date and   time : "),19);
                        end_time[19]='\0';
                   }
                   
                   count++;
               } 
               fseek(fd_read,0l,SEEK_SET);
               if(x && y)
               {
                   if(!(strcmp("2001-01-01 12:12:12",end_time)>0 || strcmp(start_time,"2013-01-01 12:12:12")>0 ))
                   {
                                           
                        printf("-------------------%s---------------------\r\n",logname);
                        while(NULL != fgets(line,80,fd_read)) printf( "%s",line);
                   }
                       
               
                   res = 0;
               } 
               fclose(fd_read);
          }
      
}
void printout_log_r(log_record_t *log_record)
{
    int start_index = 0;
    int i;
    int r;
    int log_index;
    int end_index;
    if(NULL == log_record) return;
    printf("--------------------------R--------------------------\r\n");
    if(log_record->is_r_full) 
          start_index = (log_record->current_r_index == log_record->last_r_index)
                         ? 0 : log_record->current_r_index+1; 
          
    for(i = start_index;i < log_record->last_r_index + start_index;i++)   
    {
        if(i > log_record->last_r_index) log_index = i - log_record->last_r_index;
        else log_index = i;
                  
        read_from_log_head(log_index);    
    }   
    
    
}
void printout_log_i(log_record_t *log_record)
{
     int start_index = 0;
     int i;
     int r;
     int log_index;
     int end_index;
     if(NULL == log_record) return;
     printf("--------------------------I--------------------------\r\n");
     start_index = log_record->last_r_index + 1;
     
     if(log_record->is_i_full) 
          start_index = (log_record->current_i_index == log_record->last_i_index)
                        ? log_record->last_r_index + 1 
                        : log_record->current_i_index+1; 
     end_index = log_record->last_i_index + start_index - log_record->last_r_index;
     for(i = start_index;i < end_index;i++)   
     {
         if(i > log_record->last_i_index) log_index = i-(end_index-start_index);
         else log_index = i;
          
         read_from_log_head(log_index);    
     }
}
int main(int argc, char *argv[])
{

  log_record_t log_record; 
  int cmd = 2;  

  read_from_seq(&log_record);
  switch (cmd)
  {
         default:
         case 0:              
              printout_log_r(&log_record);
              break;
         case 1:
              printout_log_i(&log_record);
              break;
         case 2:
              printout_log_r(&log_record);
              printout_log_i(&log_record);
              break;
  }    
  system("PAUSE");	
  return 0;
}