我正在尝试使此代码正常工作。它必须从文本文件中取一行,使用正则表达式来捕获我需要的信息(IP,日期,“请求”和代码)。正则表达式系统工作,我在while循环结束时拥有我想要的所有字符串。但是当我尝试将它们分配给结果列表中的字符串,并尝试打印它们时,一切都变得疯狂。
这是我管理的行的示例: “46.252.157.14 - - [18 / Dec / 2013:00:00:01 +0100]”GET / f0?fid = 21&amp; os = 0&amp; cdl = 0&amp; id = SOR HTTP / 1.1“200 17823”; < / p>
This is the output:
Line: 1 IP:46.252.157.14
Data:[18/Dec/2013:00:00:01 +0100]
REQ:"GET /f0?fid=21&os=0&cdl=0&id=SOR HTTP/1.1
COD: 200
Print List:
IP: 46.252.157.14 - - [18/Dec/2013:00:00:01 +0100] "GET /f0?fid=21&os=0&cdl=0&id=SOR HTTP/1.1" 200
Data: [18/Dec/2013:00:00:01 +0100] "GET /f0?fid=21&os=0&cdl=0&id=SOR HTTP/1.1" 200
Req: "GET /f0?fid=21&os=0&cdl=0&id=SOR HTTP/1.1" 200
Cod: 200
IP: (null)
Data: (null)
Req: (null)
Cod: (null)
正如您所看到的,字符串都正确打印,当我尝试在列表中打印字符串时出现问题。
这是代码:
#include <string.h>
#include <stdio.h>
#include <regex.h>
#include <stdlib.h>
struct results{
char* IP;
char* Data;
char* Req;
char* cod;
struct results* next;
struct results* back;
};
typedef struct results results;
regex_t regex_ip;
regex_t regex_data;
regex_t regex_req;
regex_t regex_cod;
char* regex_ip_re= "^[0-9]*[.][0-9]*[.][0-9]*[.][0-9]*";
char* regex_data_re="\\[.*?\\]";
char* regex_req_re="(\".*?)\"";
char* regex_cod_re="\\s[0-9]{3}\\s";
char* line=NULL;
char* match(regex_t* r,char* to_match){
char* result;
regmatch_t regmatch[strlen(line)];
if (regexec(r, to_match, strlen(line),regmatch, 0) == 0)
{
int g = 0;
for (g = 0; g < strlen(line); g++)
{
if (regmatch[g].rm_so == (size_t)-1)
break; // No more groups
char sourceCopy[strlen(to_match) + 1];
strcpy(sourceCopy, to_match);
sourceCopy[regmatch[g].rm_eo] = 0;
result=sourceCopy+regmatch[g].rm_so;
}
}
return result;
}
void createnode(results* node){
results *temp=malloc(sizeof(results));
temp->next=NULL;
temp->back=node;
node->next=temp;
}
int main(){
regcomp(®ex_ip,regex_ip_re, REG_EXTENDED|REG_NEWLINE);
regcomp(®ex_data,regex_data_re, REG_EXTENDED|REG_NEWLINE);
regcomp(®ex_req,regex_req_re,REG_EXTENDED|REG_NEWLINE);
regcomp(®ex_cod,regex_cod_re,REG_EXTENDED|REG_NEWLINE);
FILE *log;
char* IP=;char* DATA;char* REQ;char* COD;
results *output=malloc(sizeof(results));
results *head=output;
size_t len=NULL;
log=fopen("./log.txt","r");
FILE *out=fopen("./output.txt","a");
int linenum=1;
//This is where i think the problem is:
while(!feof(log)&&linenum<2){
getline(&line,&len,log);
IP=match(®ex_ip,line);
printf("Line: %d IP:%s\n",linenum,IP);
output->IP=IP;
DATA=match(®ex_data,line);
printf(" Data:%s\n",DATA);
output->Data=DATA;
REQ=match(®ex_req,line);
printf(" REQ:%s\n",REQ);
output->Req=REQ;
COD=match(®ex_cod,line);
printf(" COD:%s\n",COD);;
output->cod=COD;
linenum++;
createnode(output);
output=output->next;
}
output=head;
printf("\nPrint List:\n\n");
while(output!=NULL){
printf("IP: %s\nData: %s\nReq: %s\nCod:
%s\n\n",output->IP,output->Data,output->Req,output->cod);
output=output->next;
}
return 0;
}
我试图尽可能清楚地报告整个代码,即使我认为问题只是列表。对不起,如果难以阅读。
谢谢;)
编辑,最终结果:
列表声明。
struct results{
char IP[15];
char Data[30];
char Req[700];
char cod[5];
struct results* succ;
struct results* prec;
};
typedef struct results results;
这是一段时间
while(!feof(log)){
getline(&line,&len,log);
strcpy(output->IP,match(®ex_ip,line));
strcpy(output->Data,match(®ex_data,line));
strcpy(output->Req,match(®ex_req,line));
strcpy(output->cod,match(®ex_cod,line));
linenum++;
creanodo(output);
output=output->succ;
}
return testa;
}
答案 0 :(得分:1)
您正在存储指向超出范围的瞬态C字符串(sourceCopy
)的指针。
稍后访问它们是UB。
创建字符串的副本(malloc + strcpy),然后将它们存储在列表中。