2 Replies - 1204 Views - Last Post: 03 May 2013 - 06:29 AM Rate Topic: -----

#1 kumarrags2004  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-December 10

entering data from a txt file into a linked list

Posted 03 May 2013 - 05:34 AM

hello
the code is for creating a shop purchase, stock maintainence system
i have a problem in entering data from txt file to linked list with fscanf(fp,............) function;
the code below has the problems written in comments section but breifly, when i run the code in turbo c and enter the data at run time the contents go in the file correctly if i am not reading in older contents. every time i open the program the file is overridden.
when i read in the contents with fscanf() junk values start to get added in the file i dont know why. may be because i am using pointers instead of objects, but i do not know another way.
i know that my program is inefficient but still i want to know what is the problem with the code and how to resolve it
i have used many variables , some of them might never be used ,pls forgive me.
problem in the code will be found in the create function:

#include<alloc.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<graphics.h>
#define size 20
struct shop{
char name[size];
int quantity;
int price;
struct shop *next;
}*start;
typedef struct shop sh;

char u_name[30];
char u_pass[30];

int i,user,password;
int k=0,l=0;
char add_more;
char c,choice,c1,more;




void create()
{ FILE *fc,*fp;

struct shop *ptr,*temp,*g,*l,*m,*t,*i,*d;
char ch,v[20];
int r,z,w,flag=0;
//the code ***************from here******************
fc=fopen("storedata.txt","r");
d=(sh*)malloc (sizeof(sh));
d->next=NULL  ;
i=d;
m=d;


while(fscanf(fc,"%s\t%d\t%d",m->name,m->quantity,m->price)!=EOF)
{
d=(sh*)malloc (sizeof(sh));
m->next=d;
m=m->next;

}
m->next=NULL;
fclose(fc);

t=i;

clrscr();
printf("NAME\t\t\tQUANTITY\t\t\t\tPRICE(RS)");

do
{
printf("\n%s ",t->name);
printf("\t\t\t%-20d",t->quantity);
printf("\t\t\t%-40d",t->price);
t=t->next;
}while(t!=NULL);
 getch();
 getch();
//*************till here********the smaller code part above is the code to read in the file which doesnt work correctly
start=i;  // when i remove this line all the values entered in the file are correct but file is overridden every time i run it
ptr=start;
g=start;
l=start;
ch='y';
do
{
if(start!=NULL)
{
while(ptr->next!=NULL)
{
ptr=ptr->next;
}


printf("\nEnter the name of the item: ");
scanf("%s",&v);
printf("\nEnter the quantity of the item: ");
scanf("%d",&z);
printf("\nEnter the price of the item('0'if item present): ");
scanf("%d",&w);
while(g->next!=NULL)
{
if(strcmp(v,g->name)==0)
{
g->quantity=(g->quantity+z);
flag=1;

}
g=g->next;
}
if(g->next==NULL)
{
if(strcmp(v,g->name)==0)
{
g->quantity=(g->quantity+z);
flag=1;

} }
g=start;
if(flag==0)
{
temp=(sh*)malloc (sizeof(sh));
strcpy(temp->name,v);
temp->quantity=z;
temp->price=w;
ptr->next=temp;
ptr=ptr->next;
ptr->next=NULL;
 }

printf("\nDo you want to add more nodes Y/N: ");
fflush(stdin);
scanf("%c",&ch);
flag=0;

}
else
if(start==NULL)
{

temp=(sh*) malloc (sizeof(sh));


printf("\nEnter the name of the item:  ");
scanf("%s",&temp->name);
printf("\nEnter the quantity of the item:  ");
scanf("%d",&temp->quantity);
printf("\nEnter the price of the item: ");
scanf("%d",&temp->price);
start=temp;
ptr=start;
g=start;
printf("\nDo you want to add more nodes Y/N: ");
fflush(stdin);
scanf("%c",&ch);
ptr->next=NULL;

}

}while(ch!='n');
fp=fopen("storedata.txt","w");
l=start;
while(l!=NULL)
{
fprintf(fp,"%s\t%d\t%d\n",l->name,l->quantity,l->price);
l=l->next;
}
fclose(fp);
}







void show()
{
FILE *fp;
int c;
clrscr();
fp=fopen("STOREDATA.TXT","r");
c=getc(fp);
while(c!=EOF)
{
putchar(c);
c=getc(fp);

}
fclose(fp);
}


int check(struct shop *m)
{
FILE *fp;
fp=fopen("storedata.txt","r");
while(fscanf(fp,"%s\t%d\t%d\n",m->name,m->quantity,m->price)!=EOF)
{
m=m->next;
}
fclose(fp);
return 0;
}


void delete()
{
FILE *fp;
char n[size],ch;
int k,j=0,i=0;
struct shop *temp,*l,*m,*f,*r,*a;
do
{
temp=start;

printf("\nEnter the item name you want to delete: ");
scanf("%s",&n);
printf("\nEnter the quantity: ");
scanf("%d",&k);

do
{

if(strcmp(n,temp->name)==0)
{
if(k==temp->quantity)
{
if(temp==start)
{
l=temp;
r=l;
l=l->next;
start=l;
free(r);

}
else
if(temp->next==NULL)
{
l=start;
for(i=1;i<j;i++)
{
l=l->next;
}
l->next=NULL;
free(temp);
}
else
{
l=start;
for(i=1;i<j;i++)
{
l=l->next;
}
f=temp->next;
l->next=f;
free(temp);
}

}
else
if(k!=temp->quantity)
{
temp->quantity=(temp->quantity-k);
printf("\nNow the number of items of %s are: %d",temp->name,temp->quantity);
}
break;
}
temp=temp->next;
j=j+1;
}while(temp!=NULL);
printf("\n\nDo you want to delete more items: ");
fflush(stdin);
scanf("%c",&ch);
}while(ch!='n');
fp=fopen("storedata.txt","w");
a=start;
while(a!=NULL)
{
fprintf(fp,"%s\t%d\t%d\n",a->name,a->quantity,a->price);
a=a->next;
}
fclose(fp);
}


void purchase()
{
FILE *fp;
char n[size],ch;
int k,j=0,i=0,s=0;
struct shop *temp,*l,*m,*f,*r,*a;
do
{
temp=start;

printf("\nEnter the item name you want to buy: ");
scanf("%s",&n);
printf("\nEnter the quantity: ");
scanf("%d",&k);

do
{

if(strcmp(n,temp->name)==0)
{
if(k==temp->quantity)
{
if(temp==start)
{
l=temp;
r=l;
l=l->next;
start=l;
free(r);

}
else
if(temp->next==NULL)
{
l=start;
for(i=1;i<j;i++)
{
l=l->next;
}
l->next=NULL;
free(temp);
}
else
{
l=start;
for(i=1;i<j;i++)
{
l=l->next;
}
f=temp->next;
l->next=f;
free(temp);
}

}
else
if(k!=temp->quantity)
{
temp->quantity=(temp->quantity-k);

}
break;
}
temp=temp->next;
j=j+1;
}while(temp!=NULL);
s=s+temp->price;
printf("\n\nDo you want to add more items:");
fflush(stdin);
scanf("%c",&ch);

}while(ch!='n');
printf("\n\nYour total bill is: %d",s);
fp=fopen("storedata.txt","w");
a=start;
while(a!=NULL)
{
fprintf(fp,"%s\t%d\t%d\n",a->name,a->quantity,a->price);
a=a->next;
}
fclose(fp);

}



void header()
   {
      clrscr();
      printf("\n\t\t");
      gotoxy(18,7);
      printf("***********************************************");
      gotoxy(18,8);
      printf("\n\t\t");
      gotoxy(18,9);
      printf("*                 Goods Store                 *");
      gotoxy(18,10);
      printf("\n\t\t");
      gotoxy(18,11);
      printf("***********************************************");
      gotoxy(25,18);

      printf("Press Any Key To Continue........");
      c1=getch();

 }





void master_password()
  {

   printf("\n\n\n\n\n\n\t\t\t============================");
   printf("\n\t\t\tEnter User Name:: ");
   gets(u_name);
   printf("\t\t\tEnter Password :: ");
   gets(u_pass);
   printf("\t\t\t============================");
   printf("\n\t\t");
   printf("Verifying...");
   printf("\n\t\t");
   for(i=0;i<47;i++)
      {
       delay(5);
       cprintf("%c",987);
	 }
   user=strcmp(u_name,"user");
   password=strcmp(u_pass,"user");
   if(user==0 && password==0)
       {
  clrscr();

   printf("\n\t\t");
  printf("Successfully Logged in");

  main_menu();
       }
   else
       {
  clrscr();
    printf("\n\t\t\t");
 printf("Invalid Username or Password");

  printf("\n\n\n\n\n\n\t\t\t");
  printf("RE-");
  master_password();
      }
      }

int main_menu()
      {

       do{

  gotoxy(32,7) ;
  printf("M A I N - M E N U");
  gotoxy(32,10);


  printf("1.Add to Stock");
  gotoxy(32,12);
  printf("2.Display Stock");
  gotoxy(32,14);
  cprintf("3.Delete from Stock");
  gotoxy(32,16);
  cprintf("4.Purchase");
  gotoxy(32,18);
  cprintf("5.EXIT");
  gotoxy(32,21);
  cprintf("Enter Your Choice: ");
  choice=getche();
  clrscr();
  if(choice == 27)
  break;

   else if(choice=='1')
      {
	gotoxy(30,12);
	create();

      }
  else if (choice=='2')
	 {
    show();
    getch();
    clrscr();
    main_menu();

	 }
  else if(choice=='3')
	{
     delete();
	}
	else if(choice=='4')
	{
     purchase();

	}

	else if(choice=='5')
	{
	 exit(0);
	 }
   clrscr();

 } while(choice!=4);
  return 0;
  }

void main()
       {

   check(start);
   header();
   master_password();
   clrscr();
   getch();
       }


thanks

Is This A Good Question/Topic? 0
  • +

Replies To: entering data from a txt file into a linked list

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3617
  • View blog
  • Posts: 11,269
  • Joined: 05-May 12

Re: entering data from a txt file into a linked list

Posted 03 May 2013 - 05:54 AM

You are using Turbo C which has a pretty good darn editor which set the standard for text editors at it's time. Unlike people using 'vi', you have no excuse not to be using an indent style that you like consistently. By not using a consistent style, it's pretty hard to read your code.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6075
  • View blog
  • Posts: 23,543
  • Joined: 23-August 08

Re: entering data from a txt file into a linked list

Posted 03 May 2013 - 06:29 AM

fflush(stdin);


WRONG! Undefined behavior. Read.

void main()


WRONG! It's int main(), and you return an integer value from main. Read.

gets(u_name);


WRONG! Never, EVER use gets(). Read.

/me withholds comments on your teachers/educational system
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1