Error in deleting and renaming a file in c

Here is my code i wrote for storing details of my apps but i think i went nuts in logic so please help me with the program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct app
{
int id;
char name[200];
int price;
int instore;
};

void displaymenu();
void showdetails();
void addappdetails();
void removeappdetails();
main()
{
displaymenu();
}

void displaymenu()
{
int choice;
char a[]="------------------------Welcome to TEAM NoA App Database-----------------------";
char b[]="Menu \n 1.See app details \n 2.Add app details \n 3.Remove app details \n";
char c[]="Enter your option :";
printf("%s\n\n\n%s\n\n%s",&a,&b,&c);
struct app app1;
FILE *fp;
fp=fopen("appdata.dat","a");
if(fp=NULL)
{
printf("\nERROR IN OPENING FILE...");
exit(0);
}
scanf("%d",&choice);
if(choice==1)
{
showdetails();
}
else if(choice==2)
{
addappdetails();
}
else if(choice==3)
{
removeappdetails();
}
getch();
}

void showdetails()
{
FILE *fp;
struct app showapp;
fp=fopen("appdata.dat","r");
if(fp==NULL)
{
printf("\nERROR IN OPENING FILE...");
exit(0);
}
while(1)
{
fscanf(fp,"%d",&showapp.id);
if(showapp.id == 0)
break;
fscanf(fp,"%s",&showapp.name);
fscanf(fp,"%d",&showapp.instore);
printf("\nApp ID:%d\nApp Name:%s\nIn-store:%d",showapp.id,showapp.name,showapp.instore);

}
fclose(fp);
}
void addappdetails()
{
FILE *fp;
struct app newapp;
fp=fopen("appdata.dat","a");
if(fp==NULL)
{
printf("\nERROR IN OPENING FILE...");
exit(0);
}
printf("\n Enter app ID(integer)");
scanf("%d",&newapp.id);
printf("\n Enter app name(max 200 chars)");
scanf("%s",&newapp.name);
printf("\n Enter 0 if not in app-store and 1 if it is in app-store");
scanf("%d",&newapp.instore);
fprintf(fp,"%d \t%s\t%d\n",newapp.id,newapp.name,newapp.instore);
fprintf(fp,"%d",0);
fclose(fp);
printf("\n Successfully added in database");
}
void removeappdetails()
{
FILE *fp,*fptr;
struct app delapp;
int icd,found=0;
fp=fopen("appdata.dat","r");
if(fp==NULL)
{
printf("\nERROR IN OPENING FILE...");
exit(0);
}
fptr=fopen("temp.dat","w");
if(fptr==NULL)
{
printf("\nERROR IN OPENING FILE...");
exit(0);
}
printf("\nEnter the app id to delete");
scanf("%d",&icd);

while(1)
{
fscanf(fp,"%d",&delapp.id);
if(delapp.id==0)
{
break;
}
if(delapp.id==icd)
{
found=1;
fscanf(fp,"%s",&delapp.name);
fscanf(fp,"%d",&delapp.instore);
}
else
{
fscanf(fp,"%s",&delapp.name);
fscanf(fp,"%d",&delapp.instore);
fprintf(fptr,"%d \t%s\t%d\n",delapp.id,delapp.name,delapp.instore);
}
}
fprintf(fptr,"%d",0);
while(1)
{
fscanf(fp,"%d",&delapp.id);
if(delapp.id==0)
{
break;
}
else
{

fscanf(fptr,"%d",&delapp.id);
fscanf(fptr,"%s",&delapp.name);
fscanf(fptr,"%d",&delapp.instore);
fprintf(fp,"%d \t%s\t%d\n",delapp.id,delapp.name,delapp.instore);
}
}



if(rename("temp.dat","appdata.dat")==0)
{
printf("Successfully Done...");
}
else
{
printf("\nError in renaming");
}
if(remove("appdata.dat")==0)
{
printf("Successfully Done...");
}
else
{
printf("\nError in deleting");
}
fclose(fptr);
fclose(fp);


if(found==0)
{
printf("\nRecord not found");
}
else
{
fp=fopen("appdata.dat","w");
if(fp==NULL)
{
printf("\nERROR IN OPENING FILE...");
exit(0);
}
fptr=fopen("temp.dat","r");
if(fptr==NULL)
{
printf("\nERROR IN OPENING FILE...");
exit(0);
}
while(1)
{
fscanf(fptr,"%d",&delapp.id);
if(delapp.id==0)
{
break;
}
fscanf(fp,"%s",delapp.name);
fscanf(fp,"%d",delapp.instore);
fprintf(fptr,"%d \t%s\t%d\n",delapp.id,delapp.name,delapp.instore);

}
fprintf(fp,"%d",0);


fclose(fptr);
fclose(fp);
if(remove("temp.dat")==0)
{
printf("Successfully Done...");
}
}

}

Topic archived. No new replies allowed.