When I run the program and I want to delete a product code then this function deletes all the file.
Could you please help me?
void PRODUCT_delete()
{
int code;
FILE *stock=fopen("stock.dat","wb+");
printf("PLEASE TYPE THE CODE OF THE PRODUCT YOU WISH TO DELETE:\t");
scanf(" %d",& code);
printf("\n");
fseek(stock,0,SEEK_END);
int fl_size=ftell(stock);
int quantity= fl_size/sizeof(product);
rewind(stock);
prdct cprd= (product *)malloc (sizeof(product)*quantity);
assert(cprd);
prdct cprd1= (product *) malloc(sizeof(product)*quantity);
assert(cprd1);
fread(cprd1,sizeof(product),quantity,stock);
int i;
for(i=0;i<quantity;i++)
{
if(cprd1[i].code!=code)
{
cprd[i]=cprd1[i++];
}
else
{
continue;
}
}
fwrite(cprd,sizeof(product),quantity,stock);
fclose(stock);
free(cprd1);
free(cprd);
printf("\a THE PRODUCT DELETED!!!\n")
}