I was building a firewall simulator sort of thing. Obviously I will add rule set.So I'm using a .txt file for my rules. Sample txt records :
HOST_IP, HOST_PORT, DEST_IP, DEST_PORT
192.168.1.100, 32, 127.0.0.1, 567
192.168.1.101, 32, 127.0.0.1, 09
192.168.1.102, 32, 127.0.0.1, 64
192.168.1.103, 32, 127.0.0.1, 56
So what I wish for is that, if the file is searching Host ip, so it should search all the host IPs first. As in, as soon as a comma ( , ) is encountered, the pointer jumps to the next line and checks the IP address (before first comma) in second line. If found, it must return, else it must jump to next line after it encounters a comma ( , ) and search for the IP in the next line.
What I have tried:
#include <stdio.h>
#include <stdlib.h>
main( )
{
FILE *fp;
char c[20],*string;
fp = fopen("test.txt", "r");
if (fp == NULL)
printf("File doesn't exist\n");
else {
do {
fscanf(fp,"%s",string);
t=getc(fp);
if(strcmp(string,c)){printf("Found");fclose(fp);}
if(t==",")
putchar(c);
} while (c != EOF);
}
fclose(fp);
}