我想知道我的代码在哪里以及为什么出错,从而导致 pop 如下所示的错误消息?因为在我的终端上编译C代码可以正常工作,但是在Linux中却有所不同,所以我想知道它在哪里出错。当我输入“-1”退出购买功能时,将发生错误。非常感谢您的宝贵时间,并指出我的错误!

int purchase(void){
        char menu_code[6]; //user input meal code
        FILE *fptr; //flie pointer for receipt text file
        FILE *tfptr; //flie pointer for trans text file
        FILE *mfptr; //flie pointer for combo text file
        FILE *afptr; //flie pointer for addon text file
        int quantity=0, itemFound=0,total_quantity=0;
        float total=0, grand_total=0;
        unsigned int combo_trans=0, ala_trans=0, i;
        struct Meal meal;

        printf("Please enter a menu code (-1 to exit purchase). Example: C0001.\n");
        scanf("%s", menu_code);

        fptr = fopen("receipt.txt", "w"); //clear file before appending for each purchase
        while(strcmp(menu_code, "-1") != 0)//while loop to compare the input with sentinel value
        {
            mfptr = fopen("combo.txt", "r");//command to open a specific file
            while(!feof(mfptr))
            {
                fscanf(mfptr, "%5[^:]:%[^:]:%f:%[^\n]\n", meal.mcode, meal.name, &meal.price, meal.description);
                if(strcmp(menu_code, meal.mcode) == 0)
                {
                    printf("Enter the quantity of the meal: ");
                    scanf("%d",&quantity);
                    quantity = (quantity <= 0) ? 0 : quantity; //accepting valid values for quantity
                    total = meal.price * quantity;
                    grand_total += total;
                    total_quantity += quantity;
                    itemFound ++;
                    combo_trans++;
                    print_order(quantity, meal.name, meal.price, grand_total);

                    tfptr = fopen("trans.txt", "a+");
                    fprintf(tfptr, "%u:%u:%.2f\n", quantity, 0, total);
                    fclose(tfptr);
                    fptr = fopen("receipt.txt", "a+");
                    fprintf(fptr, "%-15u%-34s%-17.2f\n", quantity, meal.name, meal.price*quantity);
                    fclose(fptr);

                }
            }fclose(mfptr);//closes the file after execution

            if(itemFound == 0)//start of if statement
            {
                afptr = fopen("addon.txt", "r");
                while(!feof(afptr))
                {
                    fscanf(afptr, "%5[^:]:%[^:]:%f:%[^\n]\n", meal.mcode, meal.name, &meal.price, meal.description);
                    if(strcmp(menu_code, meal.mcode) == 0)
                    {
                       printf("Enter the quantity of the meal: ");
                       scanf("%d",&quantity);
                       quantity = (quantity <= 0) ? 0 : quantity;
                       total = meal.price * quantity;
                       grand_total += total;
                       total_quantity += quantity;
                       itemFound ++;
                       ala_trans++;
                       print_order(quantity, meal.name, meal.price, grand_total);

                       tfptr = fopen("trans.txt", "a+");
                       fprintf(tfptr, "%u:%u:%.2f\n", 0, quantity, total);
                       fclose(tfptr);
                       fptr = fopen("receipt.txt", "a+");
                       fprintf(fptr, "%-15u%-34s%-17.2f\n", quantity, meal.name, meal.price*quantity);
                       fclose(fptr);
                    }
                }fclose(afptr);
            }//end of if statement
            if(itemFound == 0){
                printf("Invalid menu code!\n");
            }
            itemFound = 0;
            printf("Please enter a menu code (-1 to exit purchase). Example: C0001.\n");
            scanf("%s", menu_code);
        };//end of while loop
        fclose(fptr);
        print_receipt(ala_trans, combo_trans, total_quantity, grand_total);
        order();
        return 0;
    }

错误:
*** glibc detected *** ./a.out: double free or corruption (out): 0x09c392d8 ***
======= Backtrace: =========
/lib/libc.so.6(+0x70b81)[0xb75fbb81]
/lib/libc.so.6(+0x732d8)[0xb75fe2d8]
/lib/libc.so.6(fclose+0x14a)[0xb75eb6fa]
./a.out[0x804a4d6]
./a.out[0x804a693]
./a.out[0x804879f]
/lib/libc.so.6(__libc_start_main+0xe6)[0xb75a1d36]
./a.out[0x8048701]
======= Memory map: ========
08048000-0804c000 r-xp 00000000 fd:02 12853280                           /home/0333120/Assignment/a.out
0804c000-0804d000 rw-p 00003000 fd:02 12853280                           /home/0333120/Assignment/a.out
09c39000-09c5a000 rw-p 00000000 00:00 0                                  [heap]
b7567000-b7584000 r-xp 00000000 fd:02 12314805                           /lib/libgcc_s-4.4.7-20120601.so.1
b7584000-b7585000 rw-p 0001d000 fd:02 12314805                           /lib/libgcc_s-4.4.7-20120601.so.1
b758a000-b758b000 rw-p 00000000 00:00 0
b758b000-b771b000 r-xp 00000000 fd:02 12314671                           /lib/libc-2.12.so
b771b000-b771c000 ---p 00190000 fd:02 12314671                           /lib/libc-2.12.so
b771c000-b771e000 r--p 00190000 fd:02 12314671                           /lib/libc-2.12.so
b771e000-b771f000 rw-p 00192000 fd:02 12314671                           /lib/libc-2.12.so
b771f000-b7722000 rw-p 00000000 00:00 0
b7724000-b7728000 rw-p 00000000 00:00 0
b7728000-b7746000 r-xp 00000000 fd:02 12314649                           /lib/ld-2.12.so
b7746000-b7747000 r--p 0001d000 fd:02 12314649                           /lib/ld-2.12.so
b7747000-b7748000 rw-p 0001e000 fd:02 12314649                           /lib/ld-2.12.so
b7748000-b7749000 r-xp 00000000 00:00 0                                  [vdso]
bfec9000-bfede000 rw-p 00000000 00:00 0                                  [stack]
Aborted

最佳答案

这个错误是由我猜想的运行时问题引起的(经过一点点研究,请记住,几周前我才刚开始学习C,但我仍然很陌生)。打开了2个文件(接收和传输),但逻辑上未关闭任何一个。因此导致程序崩溃并在我的购买功能之间失败。因此,请检查将文件开头和结尾正确放置在何处。
打开要写入的文件(receipt.txt)并仅在最后将其关闭是没有意义的。因此,每次写入该函数后,我将其更改为关闭,以在每次调用该函数时覆盖具有空白状态的文件。

关于c - 在Linux帐户中但未在Mac终端上运行C的错误消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47489059/

10-15 02:14
查看更多