Hello i need some help with parsing a string as i'm very new to C and only started learning it a few days ago.
I wish to parse the string
"Jan 15 05:46:07 gateway kernel: IN=eth0 OUT= MAC=00:80:c7:c3:c7:be:08:00:03:23:2a:a8:08:00 SRC=80.232.253.76 DST=80.234.144.54 LEN=48 TOS=0x00 PREC=0x00 TTL=108 ID=43600 DF PROTO=TCP SPT=3329 DPT=135 WINDOW=16384 RES=0x00 SYN URGP=0"
so that i can get what SRC equals and SPT (80.232.253.76 & 3329).
I can split a test string using strtok() using the delims " " & "=" and i know that the SRC and SPT will be in a particular token 12th & 29th however i do not know how to get these particular tokens all i'm able to do is print them off. Any help would be appriated, below is some of the code i've done already sorry if its in the wrong format.
// open the file "messages.txt" for reading
FILE *file;
file = fopen("messages.txt", "rt");
// read the first line from the file
char buffer[250];
int linenumber = 1;
while (fgets(buffer, 250, file) != NULL) {
//display info if DTP=135
if (strstr(buffer, "DPT=135") != NULL) {
printf("LINE: %d", linenumber);
printf(" CONTAINS \"DPT=135\": %s\n", buffer);
}
linenumber++;
}
// close the stream
fclose(file);
//Splits the string str into tokens using space and =
char str[] = "Jan 15 05:46:07 gateway kernel: IN=eth0 OUT= MAC=00:80:c7:c3:c7:be:08:00:03:23:2a:a8:08:00 SRC=80.232.253.76 DST=80.234.144.54 LEN=48 TOS=0x00 PREC=0x00 TTL=108 ID=43600 DF PROTO=TCP SPT=3329 DPT=135 WINDOW=16384 RES=0x00 SYN URGP=0";
char delims[] = " =";
char *result = NULL;
result = strtok(str, delims);
//printf("SRC is \"%d\"\n", result);
char resultline = 1;
while (result != NULL) {
printf("result line is \"%d\"\n", resultline);
printf("result is \"%s\"\n", result);
result = strtok(NULL, delims);
resultline++;
}