Click here to Skip to main content
15,916,180 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, 32,, 567, 32,, 09, 32,, 64, 32,, 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 {
		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)
Updated 31-Oct-16 2:37am
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
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");


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);

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