I am using dev C++. I am making this simple function calles void quan()
. It involves file handling, reading and appending. The problem is after I input something, it crashes. I dont know why and I think it is because of the fscanf
?
void quan()
{
FILE *fp,*fp1;
int q;
char name[20];
fp=fopen("inv.txt","r");
fp1=fopen("temp.txt","a");
if(fp==NULL)
{
printf("quan error");
}
else
{
printf("enter product name: ");
gets(name);
while(fscanf(fp,"%s %d %f %s",s.prod,s.quant,s.price,s.day)==4 )
{
if(strcmp(s.prod,name)!=0)
{
fprintf(fp1,"%s %d %.2f %s\n",s.prod,s.quant,s.price,s.day);
}
else
{
printf("enter quantity: ");
scanf("%d",&q);
s.quant=s.quant+q;
fprintf(fp1,"%s %d %.2f %s\n",s.prod,s.quant,s.price,s.day);
printf("\nsuccess");
}
}
}
fclose(fp);
fclose(fp1);
remove("inv.txt");
rename("temp.txt","inv.txt");
}
this is inside my inv.txt file
apple 100 20.00 01/01/2015
orange 100 10.00 01/01/2015
banana 50 15.00 01/01/2015
grapes 15 10.00 01/01/2015
fan 100 5.00 01/01/2015