- 论坛徽章:
- 0
|
程序是要从一个TXT文件中读取N行数据,每一行三组数据,IP源地址,IP目的地,协议,每一个都是用的字符串数组来保存.
形式如下:
0.000000 12.34.56.78 -> 123.456.789.123 ARP Who has 10.27.2.145? Tell 10.27.0.1
0.004664 110.27.2.145 -> 110.27.255.255 SMB_NETLOGON SAM LOGON request from client
0.004965 12.34.56.78 -> 123.456.789.123 ARP Who has 10.27.2.145? Tell 10.27.2.12
1.051745 154.369.254.454 -> 12.26.45.788 ICMP Echo (ping) request
1.052278 12.34.56.78 -> 123.456.789.123 ARP Who has 10.27.2.120? Tell 10.27.0.1
1.053302 10.27.0.6 -> 10.21.0.23 ICMP Echo (ping) reply
1.916522 FoundryN_53:cb:00 -> Broadcast ARP Who has 10.27.2.120? Tell 10.27.0.1
1.916620 10.27.0.1 -> 224.0.0.13 PIMv2 Hello
2.916280 FoundryN_53:cb:00 -> Broadcast ARP Who has 10.27.2.120? Tell 10.27.0.1
3.916073 FoundryN_53:cb:00 -> Broadcast ARP Who has 10.27.2.120? Tell 10.27.0.1
4.258571 10.216.2.18 -> 10.27.0.14 SSH Encrypted request packet len=52
4.259051 111.27.0.14 -> 123.245.789.000 SSH Encrypted request packet len=48
因为IP地址和协议有很多是重复的,没必要每一个都要输出.我想用链表把这些数据链起来,对比一下,然后输出源地址,目的地,协议和出现的的次数,精简过后的输出就比较有重点.
打开文件后,读一行数据,对比所有的链表结点,如果发现有一个节点的数据都和这一行的数据一样,那么,把这个节点里的计数器加1.当发现这一行的IP源地址,IP目的地和协议名和在链表里的每个节点都是不一样的,开一个新节点.再读下一行,再对比,直到读完文件.
但我的程序只能print出第一行的数据,别的行的都没有,程序就结束了。不知道是链表的建立哪里有问题,请帮忙看一下。
嗯,还有一个和链表里的数据做比较的代码没有写,哪位达人能帮忙完成它,多谢。
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#include <errno.h>
#define SIZE 257
#define BUFFERSIZE 1025
#define LEN sizeof(struct out_packet)
int line=0,on_counter=0,off_counter=0;
struct out_packet
{
char name[SIZE];
char source[SIZE];
char des[SIZE];
int counter;
struct out_packet *link;
};
struct out_packet *head=NULL;
struct out_packet *p1=NULL,*p2=NULL;
int main()
{
FILE *file;
char buffer[BUFFERSIZE] = "";
float time = 0;
char ipfrom[SIZE]="";
char ipto[SIZE]="";
char protocol[SIZE] = "";
char description[SIZE] = "";
if(file = fopen("logtext.txt", "r"))
{
while(!feof(file))
{
memset(buffer, 0, BUFFERSIZE);
memset(protocol, 0, SIZE);
memset(description, 0, SIZE);
if(fgets(buffer, 1024, file))
{
line++;
sscanf(buffer, "%f %s -> %s %s %[^\n]\n",
&time,
ipfrom,
ipto,
protocol,
description);
int i;
for(i=0;i <12;i++)
{
if(head==NULL)
head=p1=(struct out_packet *)malloc(LEN);
else
{
p1->link=(struct out_packet *)malloc(LEN);
p1=p1->link;
}
strcpy(p1->name,protocol);
strcpy(p1->source,ipfrom);
strcpy(p1->des,ipto);
}// for ends
p1->link=NULL;
/*
还少一些代码,如何让这一行的数据和链表里的节点做比较,
但是我不会写,哪位达人帮忙写一下吧
*/
//***** 输出链表,也不知道对不对 *************
struct out_packet *pprint;
pprint=head;
if(head!=NULL)
do
{
printf("%s from %s, to %s, %d times\n",pprint->name,pprint->source,pprint->des,pprint->counter);
pprint=pprint->link;
}while(pprint!=NULL);
}//else end
}
fclose(file);
}
printf("Total %d lines,%d packet,%d off_packet\n", line,on_counter,off_counter);
return 0;
}
} |
|