gpt4 book ai didi

c - 关于段错误的建议、有效使用 gdb、C 编程(新手)

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

我在 C 语言中遇到段错误问题,并且我无法弄清楚为什么会发生这种情况。我认为这与滥用 fget(c) 函数有关。

  while((ch = fgetc(fp))!= EOF) { 
printf("Got inside first while: character is currently %c \n",ch); //**********DELETE
while(ch != '\n') {
char word[16]; //Clear out word before beginning
i = i+1; //Keeps track of the current run thru of the loop so we know what input we're looking at.
while(ch != ' ') {
printf("%c ",ch); //**********DELETE
//The following block builds up a character array from the current "word" (separated by spaces) in the input file.
int len = strlen(word);
word[len] = ch;
word[len+1] = '\0';
printf("%s",word);
ch = fgetc(fp);
}

//The following if-else block sets the variables TextA, TextB, and TextC to the appropriate Supply Types from the input.
//This part may be confusing to read mentally, but not to trace. All it does is logically set TextA, B, and C to the 3 different possible values SupplyType.
if(word!=TextB && word!=TextC && i==1 && TextB!="") {
strcpy(TextA,word);
}
else if(word!=TextA && word!=TextC && i==1 && TextC!="") {
strcpy(TextB,word);
}
else if(word!=TextB && word!=TextA && i==1) {
strcpy(TextC,word);
}

switch(i) {
case 1:
if(TextA == word) {
SubTypeOption = 1;
}
else if(TextB == word) {
SubTypeOption = 2;
}
else if(TextC == word) {
SubTypeOption = 3;
}
break;
case 2:
//We actually ultimately don't need to keep track of the product's name, so we do nothing for case i=2. Included for readibility.
break;
case 3:
WholesalePrice = atof(word);
break;
case 4:
WholesaleAmount = atoi(word);
break;
case 5:
RetailPrice = atof(word);
break;
case 6:
RetailAmount = atoi(word);
break;
}//End switch(i)

ch = fgetc(fp);
}//End while(ch != '\n')

//The following if-else block "tallys up" the total amounts of SubTypes bought and sold by the owner.

if(SubTypeOption == 1) {
SubType1OwnersCost = SubType1OwnersCost + (WholesalePrice*(float)WholesaleAmount);
SubType1ConsumersCost = SubType1ConsumersCost + (RetailPrice *(float)RetailAmount);
}
else if(SubTypeOption == 2) {
SubType2OwnersCost = SubType2OwnersCost + (WholesalePrice*(float)WholesaleAmount);
SubType2ConsumersCost = SubType2ConsumersCost + (RetailPrice *(float)RetailAmount);
}
else if(SubTypeOption == 3) {
SubType3OwnersCost = SubType3OwnersCost + (WholesalePrice*(float)WholesaleAmount);
SubType3ConsumersCost = SubType3ConsumersCost + (RetailPrice *(float)RetailAmount);
}

}//End while((ch = fgetc(fp))!= EOF)

使用gdb(只是a.out的简单运行)我发现问题与getc有关,但它没有告诉哪一行/哪一行。但是,我的程序确实输出“第一次进入一边:字符当前是 S”。这个 S 是我输入文件中的第一个字母,所以我知道它在某种程度上按应有的方式工作,但随后导致了段错误。

有人对可能出现的问题或如何调试此问题有任何建议吗?我对 C 比较陌生,主要对语法感到困惑。我有一种感觉,我在语法上犯了一些小错误。

顺便说一句,这段代码的目的是从字符串中获取一个单词。例子:请帮我完成这个程序

应该给出等于“Help”的单词

更新:现在我遇到了一个很酷的错误(尽管很神秘)。当我重新编译时,我得到了这样的东西:

单词现在是 w S现在这个词是 w Su现在这个词是 w Sup...等等,除了它会持续一段时间,构建一个单词金字塔。

我的输入文件中仅包含字符串“SupplyTypeA 1.23 1 1.65 1”。

更新:段错误已修复(问题是,我使用 fgetc() 超出了文件末尾)。感谢大家。

如果有人仍然看到这个,他们能帮我弄清楚为什么我的输出文件不包含任何应有的正确数字吗?我想我可能在我得到的单词中误用了 atof 和 atoi。

最佳答案

确保使用 -g -O0 选项编译程序

下一步在 GDB 中逐行查看程序,观察并了解程序在做什么。看看各种变量。这是必不可少的调试技巧。

当它死掉时,输入命令“k”,这将为您提供堆栈跟踪,跟踪的最后一行将具有失败的行号,但您知道无论如何,因为您在该行上,所以您执行了一个步骤命令

关于c - 关于段错误的建议、有效使用 gdb、C 编程(新手),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14777956/

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