我遇到了在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是我输入文件中的第一个字母,所以我知道它在某种程度上应该如何应用,但随后会导致seg错误。
有没有人对可能出现的问题或如何调试此问题有任何建议?我对C比较陌生,主要是语法混淆。我有一种感觉,我做了一些小的语法错误。
顺便说一句,这段代码意味着从字符串中获取一个单词。例: 请帮助我这个程序
更新:现在,伙计们我会得到一个很酷的错误(虽然神秘)。当我重新编译时,我得到了这样的东西:
字现在是 字现在是苏 这个词现在是苏 等等,除了它持续一段时间,建立一个字的金字塔。
更新:修复了分段错误(问题是,我使用fgetc()过了文件的末尾)。感谢大家。
如果有人还在看这个,他们可以帮我弄清楚为什么我的输出文件不包含任何正确的数字吗?我想我可能会误解atof和atoi对我所得的话语。
答案 0 :(得分:2)
确保使用-g -O0选项编译程序
接下来逐步完成GDB中的程序,观察并了解您的程序正在做什么。看看各种变量。这是必不可少的调试技巧。
当它死亡时键入命令'k'这将给你一个堆栈跟踪,跟踪的最后一行将有失败的行号,但你知道,无论如何,因为你在线上,你做了一个步骤命令< / p>
答案 1 :(得分:0)
旧C中没有“fget”,但也许你正在使用一个更现代的版本,它有一个名为“fget”的东西。最有可能的是,你打算使用“fgetc”。当C I / O函数以“f”开头时,它通常需要一个FILE *句柄作为参数,就像“fgetc”那样。在阅读完文档后,请尝试使用“fgetc”。