2017-11-25 122 views
-3

我想知道我的代碼出錯的原因是什麼,它導致瞭如下所示的錯誤消息彈出?因爲在我的終端上編譯C代碼工作正常,但是它在Linux中不同,所以我想知道它出錯的地方。當我輸入「-1」退出我的購買功能時發生錯誤。非常感謝你的時間,並指出我的錯誤!在linux帳戶中運行C但不在mac終端上的錯誤消息

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 
+0

你能澄清你的編譯過程嗎?你是否用macOS編譯和運行成功,遇到編譯和運行Linux的麻煩?請注意,您在macOS中編譯的二進制文件在Linux上不起作用。 –

+1

您的代碼包含幾個錯誤。請閱讀[mcve]。 – melpomene

+0

我用macOS和linux成功編譯它。但是我在linux上運行它時遇到了問題,它在macOS上完美地找到了它。所以我想知道哪個部分導致這個錯誤發生在Linux中運行。謝謝! –

回答

0

我知道有時候人在#1並不像他們downvoted上,他們認爲過於簡單或不理解某些問題的最友好的。經過一些Google搜索之後,大部分與我有關的問題都被低估了,沒人關心,所以我找不到答案,即使我知道我最有可能會陷入低谷,我也不得不問。

我知道有很多方法可以解決問題,但是如果程序員在多次失敗後無法識別自己的問題會發生什麼?問我做錯了什麼是錯的?我認爲還有其他人也是無能爲力,並且會從這個簡單的問題中獲益匪淺。

所以,我想分享我的知識,因爲我已經設法解決我的錯誤。

這個錯誤是由我認爲的運行時間問題引起的(經過一點研究,記住,我剛剛在幾個星期前開始學習C,而我仍然對它有所瞭解)。有2個文件打開(收據和反式文件),但其中一個沒有邏輯關閉。因此導致程序在我的購買功能之間崩潰並失敗。因此,請檢查將文件打開和關閉的位置正確放置在哪裏。

打開文件寫入(receipt.txt)並僅在最後關閉文件沒有意義。所以,我在每次調用函數後寫入它以覆蓋空白狀態的文件後立即關閉它。

相關問題