I am trying to use strstr to search for any matches using a substring and comparing it to a line of text but haven't been successful in getting a match so far. I am opening and reading a file using popen while trying to do a search with only the ipv4 address looking for matches. What is wrong with my code? Any help will be much appreciated, thanks.
char *buffe = malloc(sizeof(char));
FILE *rib_file = popen("bgpdump -Mv rib.20160101.0000.bz2", "r");
while(fgets(buffe, sizeof(buffe), rib_file)!=NULL) {
if(buffe[strlen(buffe)-1] == '\n')
buffe[strlen(buffe)-1] = '\0';
for (int i = 0; i < argc; i++) {
if((strstr(buffe, argv[i])) != NULL) {
printf("A match found on line: %d\n", line_num);
printf("\n%s\n", buffe);
find_result++;
}
line_num++;
}
}
if(find_result == 0) {
printf("\nSorry, couldn't find a match.\n");
}
if (rib_file) {
pclose(rib_file);
}
free(buffe);
}
an example of what buffe is like:
TABLE_DUMP2|01/01/16 00:00:38|B|202.232.0.3|2497|223.255.254.0/24|2497 7473 3758 55415|IGP
I am trying to print the exact line as above when my code finds a match.