Hi,

I am stuck in file reading problem. I have tried various solutions but nothing worked. Actually I can't develop a logic to do so. I want to read some lines from file and pass them to structure. When reading a file you must use WHILE loop. I am posting my code and file here. I want to read from 10.10.10.1 to eDonkey and assign these values to a structure. Then start reading from the next one 10.10.10.3 to Bittorrent and so on and also adjust it to a structure. Can anyone help me in this regard.

1. TEXT FILE

10.10.10.1
10.10.10.2
1696	
1697
eDonkey
10.10.10.3
10.10.10.4
1698
1699
Bittorrent
10.10.10.5
10.10.10.6
1700
1701
gnutella
10.10.10.7
10.10.10.8
1701
1702
eDonkey
10.10.10.9
10.10.10.10
1703
1704
gnutella

2.CODE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct file_data{

	char src_ip[18];
	char dst_ip[18];
	char src_port[8];
	char dst_port[8];
	char p2p_proto[18];
	struct file_data *next;	
};


typedef struct file_data data;
data *start_node=NULL;
	data readdata();
	void buildlist(data);

int main(){

	int i=0;
	for(i=0;i<5;i++){

		buildlist(readdata());
	}

	printf("The program is working\n");
	return 0;
}

data readdata(){

	data getdata;
	FILE *fp;
	char input[18];
	int i=0;

	//getdata=(data *)malloc(sizeof(data));	
	
	fp=fopen("pktfile.txt","r");

	while(fgets(input,18,fp)!=NULL){

	if(i==0){
		
		strcpy(getdata.src_ip, input); 
		//printf("%s",getdata.src_ip);
		i++;
	}
	else if(i==1){
		
		strcpy(getdata.dst_ip, input);
		//printf("%s",getdata.dst_ip);
		i++;
	}
	else if(i==2){
		
		strcpy(getdata.src_port, input);
		//printf("%s",getdata.src_port);
		i++;
	}
	else if(i==3){
		strcpy(getdata.dst_port, input);
		//printf("%s",getdata.dst_port);
		i++;
		
	}
	else if(i==4){
		strcpy(getdata.p2p_proto, input);
		//printf("%s",getdata.p2p_proto);	
		i=0;
		getdata.next=NULL;

		return (getdata);		
		bzero(&getdata,sizeof(getdata));
	}
		
	/*else{
		
		printf("Wrong File");
		break;
	}*/

	//printf("%s",input);
	}
	fclose(fp);
}


void buildlist(data node){

	
	
	data *traverse;

	start_node=(data *)malloc(sizeof(data));
	//node=(data *)malloc(sizeof(data));
	traverse=(data *)malloc(sizeof(data));

	if(start_node==NULL){
	
		
		start_node=node;
	}
	else if(start_node->next==NULL){

		start_node->next=node;
		traverse=start_node;

	}
	else{

		traverse=start_node;

		while(traverse->next!=NULL)
		
		traverse=traverse->next;
		}

		traverse->next=node;
	}
	
	
}

Thanks in advance

Since you're always reading in 5 things at a time, read in the first five things from the file and store them in the structure, then traverse the linked list to the next node. You don't need any of this "if i == 0" business or any of those other if statements. You always know what the format of your file is, so use that knowledge to your advantage.

This is the big question for me.. At last if(i=4).. the function should return a structure. Now five structures would be formed. It means that the function would return five structures which I will include later in my List. Now how can I catch these five structures??

else if(i==4){
    strcpy(getdata.p2p_proto, input);
   //printf("%s",getdata.p2p_proto);
   i=0;
   getdata.next=NULL;
 
   return (getdata);
   bzero(&getdata,sizeof(getdata));
}
 
/*else{
 
printf("Wrong File");
break;
}*/
 
//printf("%s",input);
}
fclose(fp);
}

If you get to i=4, where do you close the file? You need to let the loop simply exit and return at the end of the function. And how does bzero() get called?

hmmm. Why I need to close the file handler. If I do so it will reopen the file and start reading from the line which I already have in the structure. How can I read the remaining strings?

the problem is you've got that "return" statement right near the end of the "else if (i==4)" block.

if you've returned from the function, neither "bzero" nor "fclose" will be called.

this is the danger of putting returns in odd places. unless you have a real reason not to, like device drivers, have one single return call at the END of the function, not somewhere up in the middle.


.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.