1 Replies - 3313 Views - Last Post: 08 April 2009 - 07:36 AM Rate Topic: -----

#1 rajbir14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-February 09

how to retrieve data from .txt file using linked list or structure in

Posted 08 April 2009 - 07:33 AM

Following is my code. My program is to simulate a dictionary using linked list and file handling. Now there's a problem to retrieve stored data from file into a linked list.Plz help me out..
/* Dictionary */

#include <stdio.h>
#include <conio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

struct node
{
	char data[20];
	char m[50];
	struct node *link;
};

struct node *dic[96];

void add(char*);
void load();
int search(char *);
void show();
void deldic();
void deletefile();
void save(struct node *);
void main()
{
	char word[20],ch;
	int i;

	clrscr( );

	while ( 1 )
	{
		clrscr( );
		printf( "\n\t\tDictionary\n" );
		printf( "\n\t\t1.Add Word.\n" );
		printf( "\t\t2.Search Word.\n" );
		printf( "\t\t3.Show Dictionary.\n" );
		printf("\t\t4.Display data from file. \n");
		printf("\t\t5.Delete saved file\n");
		printf ( "\t\t0.Exit." );
		printf ( "\n\n\t\tYour Choice ");
		scanf ( "%d", &ch );

		switch ( ch )
		{
			case 1 :

				printf("\nEnter any word : ");
				fflush(stdin);
				gets(word);
				add(word);

				break;

			case 2 :

				printf("\nEnter the word to search : ");
				fflush(stdin);
				gets(word );
				i=search(word);
				if(!i)
					printf("Word does not exists.");
				getch();

				break;

			case 3 :

				show();
				getch();

				break;
			case 4:
				load();
				getch();
				break;
			case 5:
				deletefile();
				getch();
				break;
			case 0 :

				deldic();
				exit(0);

			default :

				printf ( "\nWrong Choice" );
		}
	}
}

void save(struct node *q)
{
FILE *fp;
printf("data %s",q->data);
fp=fopen("dictionary.txt","a");
fprintf(fp,"\n%s",q->data);
fprintf(fp,"\t\t%s",q->m);

printf("Entry is saved");
fclose(fp);
}

void add ( char * str )
{
	int i, j;	struct node *q,*r, *temp=dic[j];
	char mean[30];
	i=search(str);
	if (!i)
	{
		printf("\nWord already exists.");
		getch();
		return;
	}
	q=(struct node *)malloc(sizeof(struct node));
	strcpy(q->data,str);
	q->link=NULL;

		printf("\n\nEnter the meaning(s) : ");
		gets(mean);
		strcpy(q->m,mean);
		save(q);
	  {	if ( dic [ j ] == NULL || strcmp ( dic[j]->data, str ) > 0 )

		 {
		r = dic [j];
		dic[j]=q;
		q->link=r;
		return;
		  }

	else
	{
		while ( temp != NULL )
		{
if ( ( strcmp(temp->data,str)<0) && ( (strcmp(temp->link->data,str ) > 0 ) ||temp->link==NULL ) )
			{
				q->link=temp->link;
				temp->link=q;
				return;
			}
			temp=temp->link;
		}
	} }

}

void deletefile()
{
FILE *fp;
fp=fopen("dictionary.txt","w");
printf("\nfile is deleted");
printf("\nPress any key to continue..");
fclose(fp);
}

void load()
{
FILE *fp;int i;char a[81];struct node *p;
fp=fopen("dictionary.txt","r");
if(fp==NULL)
{
 printf("Unable to open file dictionary.txt");
 exit(1);
 }
printf("\n***************CONTENTS OF THE FILE ARE :*****************\n");
printf("WORD				  MEANING\n");
while(fgets(a,82,fp)!=NULL)
{	puts(a);
   }
   fclose(fp);
   }

int search (char *str)
{
	struct node *n;
	char b[80];	int i;	struct node *t;
	FILE *fp;
	fp=fopen("dictionary.txt","r");

	while(fscanf(fp,"%s\t\t%s %s ",t->data,t->m,b)!=EOF)
	{ i=(stricmp(t->data,str));
	  if(i==0)
	   {printf("%s %s %s ",t->data,t->m,b);break;  }
	   }											t=t->link;
	   fclose(fp);

	return 1;
}


void show( )
{
	struct node *n;
	int i, j;

	printf("Word\t	 \tMeaning\n");
	for(i=0;i<=30;i++)
		printf("-");


	for(i=0;i<=25;i++)
	{
		n=dic[i];
		while(n!=NULL)
		{
			printf("\n%s\t\t%s",n->data,n->m);
			for ( j = 1; j<1; j++ )
				printf("\n\t\t%s",n->m[j]);
			n=n->link;
		}
	}
}

void deldic( )
{
	struct node *n, *t;
	int i;

	for(i=0;i<=25;i++)
	{
		n=dic[i];
		while(n!=NULL)
		{
			t=n->link;
			free(n);
			n=t;
		}
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: how to retrieve data from .txt file using linked list or structure in

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: how to retrieve data from .txt file using linked list or structure in

Posted 08 April 2009 - 07:36 AM

Please describe the details of the problem you are having.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1