从文本文件C中删除基于分隔符的行

时间:2017-11-20 12:19:01

标签: c text-files delimiter

这里有一个名为combo.txt的文本文件。我很难找到一种合适的方法来通过分隔符删除这些行。我首先会输入一个用户输入菜单代码,然后读取每一行以与文本文件菜单代码分隔符(C0001 / C0002 / C0003 ..)进行比较。如果匹配,我想删除文本文件中的特定行。我希望有人能提出任何建议让我开始,非常感谢你!

    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

到目前为止,这是我提出的,它不应该按照应有的方式工作。仅供参考。

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;
    }

}

0 个答案:

没有答案