调整log的毫秒显示格式

原来的log显示

代码修改后的log显示,由于目前只关注时间格式,目前的代码有待完善。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]){
    FILE * fp_r, *fp_w;
    int len;
    char buf[4096] = {0};
    char str[1024] = {0};
    char *res, *res1, *res2;
    char *linestr = "\n";
    char *tempstr = NULL;
    int tempnum = 0 ,num =0;

    fp_r = fopen("E:\\Test_log\\input.txt","rb+");
    if(fp_r == NULL){
        printf("fopen fp_r error! \n");
        return -1;
    }

    fp_w = fopen("E:\\Test_log\\output.txt","wb+");
    if(fp_w == NULL){
        printf("fopen fp_w error! \n");
        return -1;
    }
    while(fgets(buf, 4096,fp_r)!=NULL){
        res = buf;        
        while(*res !='2'){  /*保证首行是2019开头不是空格开头*/
            res++;
        }
        res1 = strstr(res,":");
        res1++;
        res2 = strstr(res1,":");
        
    
        tempnum =(*(res2+4))-'0'; /*res2+4为毫秒的位置*/
        printf("tempnum1= %d  ",tempnum);
        if (tempnum>=0 && tempnum <=9){
            num = tempnum;

        }

        tempnum =(*(res2+5))-'0';
        printf("tempnum2= %d  ",tempnum);
        if (tempnum>=0 && tempnum <=9){
            num = num*10+ tempnum;

        }

        tempnum =(*(res2+6))-'0';
        printf("tempnum3= %d  ",tempnum);
        if (tempnum>=0 && tempnum <=9){
            num = num*10+ tempnum;

        }
        printf("num = %d\n",num); /*毫秒值*/
        if(num >=10 && num <= 99){ /*将两位毫秒数变为3位毫秒数,如056*/
            *(res2+4 )= '0';
            tempstr =itoa(num,str,10);
            memcpy(res2+5,tempstr,2);
        }

        if(num >=0 && num <= 9){/*将一位毫秒数变为3位毫秒数,如007*/
            *(res2+4 ) = '0';
            *(res2+5 ) = '0';
            tempstr =itoa(num,str,10);
            memcpy(res2+6,tempstr,1);
        }

        *(res2+7 ) = ' '; 
        *(res2+8 ) = ' '; 
        
        len = strlen(res);
        memcpy(res+len, linestr, strlen(linestr));
        fputs(res, fp_w);
        memset(buf, 0, sizeof(buf));
        res = NULL;
        res1 =NULL;
        res2 = NULL;
        num = 0;
        tempnum = 0;
        
    }
    
    return 0;
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值