File Handling cant be handled....pls sumbody look into it
First I am new to C programming so pls dont laugh looking at my code :( I am trying to make a program in C using file handling just to store and do the calculations but my trainer says that no need to do this in C as it will require database concept to get the required results.....I kindda took it in a challenging mode just to see whether I can prepare the Simple form in C not using any database concept.....
The code is to do certain tasks done at a shopping Mall but in a very small manner....again pls dont laugh.....
I am giving the part of the code below wherein I am facing the problem for only 2 cases in switch -:
Code:
case 3:
// Customer Pricing list of the products bought
tot=0;
do {
printf("\n Enter the Product Code==> ");
scanf("%d",&code);
fp=fopen("C:/Product Details.txt","r");
ag=fopen("C:/Reprice listing.txt","r");
if(ag==NULL)
printf("\n Objection.....No such file exists...please retry");
else
{
for(i=0;i<n;i++)
{
if(q[i].newprice!=s[i].price)
//fread(&q[i],sizeof(q[i]),1,ag);
tot=tot+q[i].newprice;
}
}
if(fp==NULL)
printf("\n Objection.....No such file exists...please retry");
else
{
for(i=0;i<n;i++)
{
if(code==s[i].prcde)
tot=tot+s[i].price;
}
printf("\n Do you like to continue ? Press Y for yes==> ");
fflush(stdin);
scanf("%c",&vote);
}
}while(vote=='Y' || vote=='y');
fclose(fp);
printf("\n The total price for the customer is = INR %f",tot);
fflush(stdin);
break;
case 4:
// Below code is for re-pricing of the products price list
printf("\n Enter the product code for repricing==> ");
scanf("%d",&recno);
fp=fopen("C:/Product Details.txt","r");
ag=fopen("C:/Reprice listing.txt","w");
if(fp==NULL)
printf("\n Objection.....No such file exists...please retry");
else
{
fread(&s[i],sizeof(s[i]),1,fp);
for(i=0;i<n;i++)
{
if(recno==s[i].prcde)
{
if(ag==NULL)
printf("\n Objection.....No such file exists...please retry");
else
{
printf("\n Enter the new price = ");
scanf("%f",&q[i].newprice);
}
}
q[i].prcde1=s[i].prcde;
fwrite(&q[i],sizeof(q[i]),1,ag);
}
}
fclose(fp);
fclose(ag);
break;
Now the problem I am facing here is that in Case 4: the new price cant be overwritten on the previous Case 1:( (not given here used for taking input from user) but is getting updated in a 2nd file wherein I am showing only the product code and the change of price effected when selected Case 5 to show the result. But it erases the other data of the products which is already entered when Case 5: is selected. Case 2(not given here) will show all the fileds like product code,name and price. Now whne Case 3: is selected for pricing of the products after price change done in Case 4: it retains the old price and ADDS up with the new price and then shows the result..........I am like at total loss and need sumone who can show me the way where I can go to fix the prob....pls help thanks