user2055216
user2055216

Reputation: 61

Advice on Segmentation Fault, using gdb effectively, C Programming (newbie)

I am having a problem with a segmentation fault working in C, and I cannot figure out why this is occurring. I think it has something to do with misuse of the fget(c) function.

  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)

Using gdb (just a simple run of the a.out) I found that the problem is related to getc, but it does not tell which line/which one. However, my program does output "Got in side the first while: character is currently S". This S is the first letter in my input file, so I know it is working somewhat how it should, but then causes a seg fault.

Does anyone have any advice on what could be going wrong, or how to debug this problem? I am relatively new to C and confused mostly on syntax. I have a feeling I've done some small syntactical thing wrong.

By the way, this snippet of the code is meant to get a word from a string. Example: Help me with this program please

should give word equaling "Help"

Update: Now guys I am getting kind of a cool error (although cryptic). When I recompiled I got something like this:

word is now w S word is now w Su word is now w Sup ... etc except it goes on for a while, building a pyramid of word.

with my input file having only the string "SupplyTypeA 1.23 1 1.65 1" in it.

UPDATE: Segmentation fault was fixed (the issue was, I was going past the end of the file using fgetc() ). Thanks everyone.

If anyone still glances at this, could they help me figure out why my output file does not contain any of the correct numbers it should? I think I am probably misusing atof and atoi on the words I'm getting.

Upvotes: 0

Views: 157

Answers (2)

pm100
pm100

Reputation: 50190

Make sure you compile the program with -g -O0 options

Next step through the program line by line in GDB, watch and understand what your program is doing. Look at the various variables. This is the essential debugging skill.

WHen it dies type the command 'k' this will give you a stack trace the last line of the trace will have the failing line number, but you know that anyway because you were on the line shen you did a step command

Upvotes: 2

Ron Burk
Ron Burk

Reputation: 6231

There is no "fget" in good old C, but maybe you're using a more modern version that has something named "fget". Most likely, you meant to use "fgetc". When a C I/O function starts with "f", it usually wants a FILE* handle as an argument, as "fgetc" does. Try using "fgetc" instead, after reading the documentation for it.

Upvotes: 0

Related Questions