0

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

thanks

user rk
  • 376
  • 5
  • 13
  • 4
    Please reduce the code to a _minimal_ testcase that reproduces your problem, and learn how to use a debugger/sprinkle your code with printfs to figure out what it's doing. – Mat May 03 '13 at 12:56
  • Please shorten the code to the [relevant parts](http://sscce.org/) - and Format it. – Werner Henze May 03 '13 at 13:07
  • @Mat the relevant part is the reading of file marked with comments in the create function.i dont know how to add code blocks in this comment section . the problem is in reading the file to a linked list. sorry for the inconvenience – user rk May 03 '13 at 16:50

2 Answers2

1

Just as scanf, fscanf requires the locations/addresses where it will store the information denoted by the format argument. Following usage is not correct:

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

Since neither m->quantity nor m->price are valid addresses, you should use & operator:

fscanf(fc,"%s\t%d\t%d",m->name,&m->quantity,&m->price);
Julio Moreno
  • 211
  • 1
  • 2
  • this code that you gave is reading the contents to the linked list correctly but with some junk values. earlier everything was junk except the strings and various lines were added to the file. now the quantity and prices are being read but 1 or 2 junk blocks with junk(name price and quantity) are being added. pls help. thanks for the above help also. – user rk May 03 '13 at 16:19
  • You have to review the data that is being written to the file, I'm not able to look at the complete code anymore. But if I recall correctly I spotted a similar error in a scanf(), you were doing something like: `scanf("%s", &m->name);` , which is also wrong. – Julio Moreno May 06 '13 at 15:32
1

Your return value handling for fscanf() is off.

It doesn't return a pointer, it returns an int, see the documentation. The integer is the number of successful conversions; you should match the value to the nunber of % specifiers in your format string.

And it needs pointers to where data should be stored; an integer conversion like %d needs an address of an int, i.e. &m->quantity in your code.

Also don't cast the return value of malloc() in C. In fact, re-write

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

as:

d = malloc(sizeof *d);

for improved clarity, less repetition, and more brevity.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606