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

来源:互联网 发布:mac 触摸板 鼠标 消失 编辑:程序博客网 时间:2024/05/17 08:19
#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:00the usage of cpu is 2.2%the usage of mem is 16.678800%the usage of swap is .411200%07-05 00:15the usage of cpu is .9%the usage of mem is 16.378300%the usage of swap is .394600%07-05 00:30the usage of cpu is 12.2%the usage of mem is 17.535100%the usage of swap is .394700%07-05 00:45the usage of cpu is 12.4%the usage of mem is 17.657800%the usage of swap is .406600%
原创粉丝点击