69,336
社区成员
发帖
与我相关
我的任务
分享
if (method[i]="[GET]")
这句是要判断method[i]=="[GET]"吗?字符串应该这样判断:#include<string.h> //头文件
if(strcmp(method[i],"[GET]")==0)
#include<stdio.h>
#include<string.h>
int main(void)
{
char line[2048];
FILE *fp;
int n=0, m=0;
fp = fopen("access.log", "r");
if (!fp) {
printf("false\n");
return -1;//直接返回
}
while (fgets(line,2048,fp) != NULL)
{
//puts(line); //需要的话去掉行首的双斜线输出
n++;
if (strncmp(line, "[GET]", 5) == 0)m++;
}
printf("%.4lf", 1.0*m/n);
fclose(fp);
return 0;
}
#include<stdio.h>
#include<string.h>
int main(void)
{
FILE *fp;
fp=fopen("access.log","r+");
if(!fp){
printf("false\n");
return -1;//直接返回
}
else{
printf("true\n");
}
char method[255]={};
char ip[255]={};
char protocol[255]={};
char time[255]={};
char add[255]={};
char port[255]={};
char page[255]={};
char status[255]={};
char exploer_info[255]={};
char key[255]={};
char value[255]={};
char prams_cnt[255]={};//对字符串进行定义和初始化
struct record
{
char method[255];
char ip[255];
char protocol[255];
char time[255];
char add[255];
char port[255];
char page[255];
char status[255];
char exploer_info[255];
struct prams
{
char key[255];
char value[255];
};
char prams_cnt[255];
};//将字符串存入结构体中
while(getc(fp)!=EOF)
{
/* //可能出现内存溢出
fgets(method,1000,fp);
fgets(ip,1000,fp);
fgets(protocol,1000,fp);
fgets(time,1000,fp);
fgets(add,1000,fp);
fgets(port,1000,fp);
fgets(page,1000,fp);
fgets(status,1000,fp);
fgets(exploer_info,1000,fp);
fgets(key,1000,fp);
fgets(value,1000,fp);
fgets(prams_cnt,1000,fp);
*/
fgets(method, sizeof(method),fp);
fgets(ip, sizeof(ip),fp);
fgets(protocol,sizeof(protocol),fp);
fgets(time, sizeof(time),fp);
fgets(add, sizeof(add),fp);
fgets(port, sizeof(port),fp);
fgets(page, sizeof(page),fp);
fgets(status, sizeof(status), fp);
fgets(exploer_info, sizeof(exploer_info), fp);
fgets(key, sizeof(key), fp);
fgets(value, sizeof(value), fp);
fgets(prams_cnt, sizeof(prams_cnt), fp);
printf("%s",method);
printf("%s",ip);
printf("%s",protocol);
printf("%s",time);
printf("%s",add);
printf("%s",port);
printf("%s",page);
printf("%s",status);
printf("%s",exploer_info);
printf("%s",key);
printf("%s",value);//将日志文件输出
}//;
int i=0,n=0;
/*
for(i=0;i<1000;i++)
{
//if (method[i]="[GET]")
if (strcmp(method, "[GET]") == 0)
{
n++;
}
};
*/
if (strcmp(method, "[GET]") == 0)
{
n++;
}
double pro=0.0;
pro=1.0*i/n;
printf("%f",pro);
int fclose(FILE *fp);
return 0;
}