Someone just brought it to my attention that I did not post my delete module...well here it is...
Code:
int dump(void){
FILE *temp, *Inventory;
char chem[40]={'\0'},name[40]={'\0'},grade[6]={'\0'},units[3]={'\0'},notes[50]={'\0'};
float addchem[7],delchem[7];
char selection;
int found;
if((Inventory=fopen("/Inventory.txt","r"))==NULL) {
printf("Cannot open file!\n");
exit(EXIT_FAILURE);
}
if((temp=fopen("/temp.$TM","w"))==NULL) {
printf("Cannot open file!\n");
exit(EXIT_FAILURE);
}
while(!getchar()=='\n');
printf("Enter Chemical to delete: ");
fgets(chem,sizeof(chem),stdin);
rewind(Inventory);
found=0;
fscanf(Inventory, "%[^,]%*c%[^/]%*c%f%*c%f%*c%[^/]%*c%[^;]%*c", name,grade,&addchem[7],&delchem[7],units,notes);
while (!feof(Inventory)){
if(strcmp(chem,name)==0)
printf("Chemical: %s\n", name);
printf("Grade: %s\n", grade);
printf("Added: %.2f%s\n", addchem[7],units);
printf("Deleted: %.2f%s\n", delchem[7],units);
printf("Notes: %s\n\n", notes);
/*** This bit of code cofirms deletion of item from inventory */
printf("\nEnter 'Y' to %s or 'N' to Cancel : ",chem);
selection=toupper(getchar());
fflush(stdin);
switch(selection){
case 'Y':
if(strcmp(chem,name))
fprintf(temp,"%s,%s/%f/%f/%s/%s;",name,grade,addchem[7],delchem[7],units,notes);
printf("You have successfully deleted %s",chem);/* test statement. Replace with file writes */
fclose(Inventory);
fclose(temp);
unlink("/Inventory.txt");
rename("/temp.$TM","/Inventory.txt");
printf("Press any 'Enter' to continue...");
getchar();
while(getchar()!='\n');
main();
break;
case 'N':
printf("Press any 'Enter' to continue...");
getchar();
while(getchar()!='\n');
main();
break;
}
}
printf("'%.*s' not found in Inventory!!!",strlen(chem)-1,chem);
printf("\nPress any 'Enter' to continue...");
getchar();
while(getchar()!='\n');
main();
return 0;
}
I want to only delete the entry i searched for however this function is clearing my entire inventory file.