Here I have a text file named combo.txt. I have a trouble finding a suitable way to delete the lines by delimiter. I would first take in a user input menu code, then read each line to compare with text file menu code delimiter ( C0001/C0002/C0003..). If it matches, I would like to delete that specific line in the text file. I hope anyone could suggest anything to get me started, thank you so much!
C0001:Lunch Deal Set A:10.99:Burger and drink
C0002:Lunch Deal Set B:12.99:Burger, fries, and drink
C0003:Teatime Saver:6.99: Nugget and drink
C0004:Dinner Deal Set A:12.99:2 pcs fried chicken and drink
C0005:Dinner Deal Set B:14.99:2 pcs fried chicken, salad, and drink
So far, this is what I came up with, it's not working the way it should be. Just for some reference.
void delete_meal()
{
FILE *cfptr;//declaring a file pointer for combo
FILE *afptr;//declaring a file pointer for combo
FILE *dtemp;
FILE *tempr;
char mcode[6];
struct Meal meal;
int type = 0; //Not found
//does the meals have to start as C for combo and A for addon and always 5 chars only?
printf("----------------------------\n");//function for deleting a data in the file
printf(" DELETE MEAL\n");
printf("----------------------------\n");
printf("Enter meal code:\n");
scanf("%s", mcode);//user input
cfptr = fopen("combo.txt", "r");//creating a new file
while(!feof(cfptr))
{
fscanf(cfptr, "%5[^:]:%[^:]:%f:%[^\n]\n", meal.mcode, meal.name, &meal.price, meal.description);
if(strcmp(mcode, meal.mcode) == 0)
{
type = 1; //combo
}
//fscanf(cfptr, "%5[^:]:%[^:]:%f:%[^\n]\n", meal.mcode, meal.name, &meal.price, meal.description);
}fclose(cfptr);
//Couldn't found it yet, look for this item in the addon.txt
if(type == 0){
afptr = fopen("addon.txt", "r");
while(!feof(afptr))
{
fscanf(afptr, "%5[^:]:%[^:]:%f:%[^\n]\n", meal.mcode, meal.name, &meal.price, meal.description);
if(strcmp(mcode, meal.mcode) == 0)
{
type = 2; //adoon
}
//fscanf(afptr, "%5[^:]:%[^:]:%f:%[^\n]\n", meal.mcode, meal.name, &meal.price, meal.description);
}fclose(afptr);
}
printf("%d", type);
switch(type){//switching to search which value matches the input
case 1:
dtemp = fopen("deletetemp.txt", "w");//opening file for write.
int found = 0;
cfptr = fopen("combo.txt", "r");
while(!feof(cfptr))
{
fscanf(cfptr, "%5[^:]:%[^:]:%f:%[^\n]\n", meal.mcode, meal.name, &meal.price, meal.description); //reading from the file
if(strcmp(mcode, meal.mcode) != 0)
{
fscanf(dtemp, "%5[^:]:%[^:]:%f:%[^\n]\n", meal.mcode, meal.name, &meal.price, meal.description);
}
fscanf(cfptr, "%5[^:]:%[^:]:%f:%[^\n]\n", meal.mcode, meal.name, &meal.price, meal.description);
}
fclose(cfptr);
remove("combo.txt");//removing one file and replacing it with another
rename("deletetemp.txt", "combo.txt");
fclose(dtemp);//file close
printf("Deletion was successful.\n");
break;
case 2:
dtemp = fopen("deletetemp.txt", "w");
afptr = fopen("addon.txt", "r");//opening a file to read from it
while(!feof(afptr))
{
fscanf(afptr, "%5[^:]:%[^:]:%f:%[^\n]\n", meal.mcode, meal.name, &meal.price, meal.description);
if(strcmp(mcode, meal.mcode) != 0)
{
fprintf(dtemp, "%s:%s:%f:%s\n", meal.mcode, meal.name, meal.price, meal.description);
}
fscanf(afptr, "%5[^:]:%[^:]:%f:%[^\n]\n", meal.mcode, meal.name, &meal.price, meal.description);
}
fclose(afptr);
remove("addon.txt");
rename("deletetemp.txt", "addon.txt");
fclose(dtemp);
printf("Deletion was successful.\n");
break;
default:
printf("This is an invalid meal code.\n");//for default value
break;
}
}