Click here to Skip to main content
15,566,262 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C++
#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==",")//here i need to jump to next line

        	 putchar(c); /* display it on the monitor
       		*/
       	
	} while (c != EOF); /* repeat until EOF (end of file)
     */
     }
    fclose(fp);
   }
Posted
Updated 31-Oct-16 3:37am
v2
Comments
Jochen Arndt 31-Oct-16 8:34am    
I suggest to use a different approach:

Load the complete file content into memory and process it there. It will be much simpler than using file handle based operations. Note that using string functions on such buffers requires allocating one more byte and setting that (the last one) to zero.

Then use strchr(), strstr(), and optionally strpbrk() and strrchr() to locate positions of interest.

For example:
If you got a position of interest and want to jump to the next line, search for the next occurence of '\n' using strchr() and add one if found.
Suvendu Shekhar Giri 31-Oct-16 8:38am    
What is the issue with the code you have tried?
«_Superman_» 1-Nov-16 0:18am    
Since you're using C++, I recommend you take at look at regular expressions (std::regex).

1 solution

Don't use fscanf, but use fgets to read one line at a time. You can then tokenise the line (using strtok) to check individual fields.
 
Share this answer
 
Comments
Member 12089703 31-Oct-16 9:50am    
#include <string.h>
#include <stdio.h>

int main()
{
FILE *fp;
char str[20],*string,t[20];
fp = fopen("test.txt", "r");
if (fp == NULL)
printf("File doesn't exist\n");
else{
while(!feof(fp)){

if(fgets(str,20,fp)!=NULL){

const char s[2] = ",";
char *token;

/* get the first token */
token = strtok(str, s);

/* walk through other tokens */
while( token != NULL )
{
printf( "%s\n", token );
token = strtok(NULL, s);
}
}
}}
return(0);
}



So I did what u asked me to.
Now what.? How can i read only the first field of every line.? Just the first line.?
Richard MacCutchan 31-Oct-16 10:41am    
If you are really serious about creating a firewall simulator, as stated in your question, then you really need to spend a lot more time learning the language that you plan to use. All of these functions are fully documented on MSDN: String Manipulation (CRT)[^]
Member 12089703 31-Oct-16 12:43pm    
I was making this as my school project, and as per my school rules, im restricted to use only C language for this semester's project.
If you can help, it would be much appreciated.
Richard MacCutchan 31-Oct-16 14:00pm    
You need to go to the link I gave you and study the string functions that will help you read and parse your file. You need to understand the basics before you can write a more advanced program. And you will learn much faster by reading the documentation than by posting questions here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900