找出文件中每小时最大利用率值

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

void jobs(char *arg1,FILE *fp) {
        int i,ret;
        char *p,*str,str1[1024]="0";
        char readbuf[1024];
        char c;
        int n=0;
        float max=0,value;

        while(fgets(readbuf,1023,fp)!=NULL) {
                if(strstr(readbuf,arg1)) {
                        n++;
                        for(p=strtok(readbuf," ");p;p=strtok(NULL," "))
                                str=p;
                        str[strlen(str)-2]=0;
                /*
                        sprintf(&c,"%c",str[0]);
                        if(c=='.') {
                */
                        if(str[0]=='.') {
                                strcat(str1,str);
                                value=atof(str1);
                                //      printf("%.2f\n",atof(str1));
                        }
                        else {
                                value=atof(str);
                                //      printf("%.2f\n",atof(str));
                        }

                        if(max<value) max=value;
                        if(n%4==0) printf("%.2f\n",max);
                        strcpy(str1,"0");
                }
        }

        fseek(fp,0,SEEK_SET);

        printf("\n");
}

 

int main(int argc,char *argv[]) {
        int i,ret;
        FILE *fp;

        if(argc<2) {
                printf("%s filename...\n",argv[0]);
                exit(-1);
        }

        for(i=1;i<=argc-1;i++) {
                fp=fopen(argv[i],"r");
                if(fp==NULL) {
                        fprintf(stderr,"fopen() error.\n");
                        exit(-1);
                }

                printf("file=%s\n%s max:\n",argv[i],"cpu");
                jobs("cpu",fp);
                //fseek(fp,0,SEEK_SET);

                printf("%s max:\n","mem");
                jobs("mem",fp);
                //fseek(fp,0,SEEK_SET);

                printf("%s max:\n","swap");
                jobs("swap",fp);
                //fseek(fp,0,SEEK_SET);

                ret=fclose(fp);
                if(ret==EOF) {
                        fprintf(stderr,"fopen() error.\n");
                        exit(-1);
                }

                printf("-------------------------\n");
        }

        exit(0);
}

 

文件内容:

$ head 0705.txt-bak (每15分钟一次) 
07-05 00:00
the usage of cpu is 2.2%
the usage of mem is 16.678800%
the usage of swap is .411200%
07-05 00:15
the usage of cpu is .9%
the usage of mem is 16.378300%
the usage of swap is .394600%
07-05 00:30
the usage of cpu is 12.2%
the usage of mem is 17.535100%
the usage of swap is .394700%
07-05 00:45
the usage of cpu is 12.4%
the usage of mem is 17.657800%
the usage of swap is .406600%
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值