gpt4 book ai didi

c - 在 linux 帐户中运行 C 但在 mac 终端上运行 C 时出现错误消息

转载 作者:行者123 更新时间:2023-11-30 21:06:41 25 4
gpt4 key购买 nike

我想知道我的代码在哪里以及为什么出错,导致弹出如下所示的错误消息?因为在我的终端上编译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 帐户中运行 C 但在 mac 终端上运行 C 时出现错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47489059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com