在Linux帐户中运行C但在mac终端上运行的错误消息

时间:2017-11-25 18:05:44

标签: c compiler-errors

我想知道我的代码在哪里以及为什么出错导致出现如下所示的错误消息?因为在我的终端上编译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

1 个答案:

答案 0 :(得分:0)

我意识到Stackoverflow中的人有时并不是最友好的,因为他们对某些他们认为过于简单或不可理解的问题进行了投票。经过一些谷歌搜索后,与我相关的大多数问题被贬低,没有人关心,所以我找不到答案,我不得不问,即使我知道我很可能会被投票。

我知道有很多方法可以解决问题,但如果程序员在多次失败后无法识别自己的问题会怎么样?问我做错了什么不对?我认为还有其他人也是无能为力的,并且从这个简单的问题中获益匪浅。

所以,我想分享我对此的了解,因为我已经设法解决了我的错误。

这个错误是由我想的运行时问题造成的(经过一些研究后,请记住,我刚刚开始在几周前开始学习C而且我还是新的)。打开了2个文件(收据和转发),但其中一个文件未在逻辑上关闭。因此导致程序崩溃并在我的购买功能之间失败。因此,请检查文件打开和关闭的位置。

打开要写入的文件(receipt.txt)并在最后关闭它是没有意义的。所以,我把它改为关闭后立即关闭每次调用函数时用空白状态覆盖文件。