Hi All,

I am trying to read a file line by line. If a line contains the following character ':' then that line will be parsed word by word and put in a list.

I am trying to do that but the head pointer is changed when we go to the next line. can anyone correct this pgm.

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

struct emplist
{
	char *name;
	struct emplist *next;
};
struct emplist *head;

int makeword();
void addtolist(char *word);
void display(struct emplist **head);
int main()
{
	//struct emplist *head;

	//head=NULL;

	//addtolist(&head);
	makeword();
	//addtolist();
	
	//display(&head);

	return 0;
}

int makeword()
{
	FILE *fp;
	
	char *flag,*word;
	char line[1024];
	int cnt=0;
	
	//head=NULL;
//	first=trav=last=NULL;
	flag=word=NULL;
	


	fp=fopen("test.txt","r+");
	if(fp==NULL)
	{
		printf("File open error");
		return 0;
	}
	
	while(fgets(line,1024,fp)!=NULL)
	{
		if(fp==NULL)
			return 0;

		flag=strchr(line,':');
		if(flag !=NULL) 
		{
			//printf(" flag data == %s\n",flag);
			word=strtok(line,"\t\v: ");
			while(word!=NULL)
			{
				addtolist(word);
				
				/*if(head==NULL)
				{
					head=(struct emplist *)malloc(sizeof(struct emplist));
					head->name=word;
					printf("first name == %s\n",head->name);
					head->next=NULL;
					

				}
				else
				{
					first=head;
					while(first->next!=NULL)
					{
						first=first->next;
					}
					last=(struct emplist *)malloc(sizeof(struct emplist));
					last->name=word;
					last->next=NULL;
					first->next=last;
				} */
				word=strtok(NULL,"\t\v\n: ");
				
			}
			
		}
		
	}

}

void addtolist(char *word)
{
	struct emplist *first,*trav,*last;
	first=trav=last=NULL;
	if(head==NULL)
	{
		head=(struct emplist *)malloc(sizeof(struct emplist));
		head->name=word;
		head->next=NULL;
		printf("head name == %s\n",head->name);
	}
	else
	{
		first=head;
		while(first->next!=NULL)
		{
			first=first->next;
		}
		last=(struct emplist *)malloc(sizeof(struct emplist));
		last->name=word;
		last->next=NULL;
		first->next=last;
		printf("confirm head name == %s\n",head->name);
		printf("last name == %s\n",last->name);
		printf("first name == %s\n",first->name);

	} 

	//printf(" word == %s\n",word);
}
void display(struct emplist **head)
{
	struct emplist *first,*trav;
	first=trav=NULL;

	first=*head;
	printf(" \n\n Printing list \n\n ");

	while(first->next!=NULL)
	{
		printf(" Value in first of next is : %s\n",first->name);
		first=first->next;

	}
	
	printf(" Value in first of next is : %s\n",first->name);
}

Why passing address of head? It is global to the application.

void display()
{
	struct emplist *first,*trav;
	first=trav=NULL;

	first=head;
	printf(" \n\n Printing list \n\n ");

	while(first->next!=NULL)
	{
		printf(" Value in first of next is : %s\n",first->name);
		first=first->next;
	}
	printf(" Value in first of next is : %s\n",first->name);
}
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.