c - read data from txt file to linked list -


the code creating shop purchase, stock maintainence system i have problem in entering data txt file linked list fscanf(fp,............) function;

the code below has problems written in comments section breifly, when run code in turbo c , enter data @ run time contents go in file correctly if not reading in older contents. every time open program file overridden. when read in contents fscanf() junk values start added in file dont know why. may because using pointers instead of objects, not know way.

i know program inefficient still want know problem code , how resolve have used many variables , of them might never used ,pls forgive me. problem in code found in 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)");  { 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 code read in file doesnt work correctly start=i;}  // when remove line values entered in file correct file overridden every time run 

thanks

just scanf, fscanf requires locations/addresses store information denoted format argument. following usage not correct:

fscanf(fc,"%s\t%d\t%d",m->name,m->quantity,m->price); 

since neither m->quantity nor m->price valid addresses, should use & operator:

fscanf(fc,"%s\t%d\t%d",m->name,&m->quantity,&m->price); 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -