views:

74

answers:

3

Possible Duplicate:
Seg Fault in Knowledge Tree

I can't pinpoint my seg fault in my information tree. It is supposed to read from file or get user input if the file does not exist. It's supposed to guess the animal based on yes or no questions. I have limited experience with c so any help would be greatly appreciated.

.c file

#include<stdio.h>
#include<stdlib.h>
#include"animal.h"
#include<string.h>
#include<assert.h>

/*returns a new node for the given value*/
struct Node * newNode (char *newValue) 
{
 printf("Node test");
 struct Node * tree;
 tree = (struct Node*)malloc(sizeof(struct Node));
 tree -> value = newStr(newValue);
 printf("Node test");
 return tree;
}


/* returns a new string with value passed as an argument*/
char * newStr (char * charBuffer)
{
 printf("Str test");
 char *newstr;
 if(charBuffer[0] == 'A' || charBuffer[0] == 'Q'){
   newstr = strdup(&charBuffer[1]); 
 }else{
  newstr = strdup("");
 }
 return newstr;
}

/*Read from a File and create a tree*/
struct Node * readATree(FILE * f)
{
 printf("ReadATree test");
      char c;
      char buffer[100];
      struct Node * newTree;
      c = fgetc(f);
      if (c == 'A'){
         fgets(buffer, 100, f);
         newTree = newNode(newStr(buffer));
  newTree -> left = NULL;
  newTree -> right = NULL;
 }
  else{
 fgets(buffer, 100, f);
 newTree = newNode(newStr(buffer));
 newTree->left = readATree(f);
 newTree->right = (struct Node *) readATree(f);
  }
      return newTree;

}

/*Write Tree to a File*/
void writeAFile(struct Node* tree, FILE * f)
{
 printf("WriteFile test");
 char buffer[100];
 strcpy(buffer, tree->value);
 if(tree != 0){
  if(tree->left == NULL && tree->right == NULL){
   fputc('A', f);
   fputs(buffer,f);
  } else{
   fputc('Q',f);
   fputs(buffer,f);
   writeAFile(tree->left, f);
   writeAFile(tree->right,f);
  }
 }
}

/*The play should start from here*/
int main (){
 printf("main test");
 struct Node* node;
 struct Node* root;
 char ans[100];
 char q[100];
 FILE * f;
 f = fopen("animal.txt", "r+");
 if(f != NULL)
  readATree(f);
 else{
  node = newNode("Does it meow?");
  node->right = NULL;
  node->right->right=NULL;
  node->left->left=NULL;
  node->left=newNode("Cat");
  root = node;
}
 while(node->left != NULL && node->right != NULL){
  printf(node->value);
  scanf(ans);
  if(ans[0] == 'Y' || ans[0] == 'y')
   node = node->left;
  else if(ans[0] == 'N' || ans[0] == 'n')
   node = node->right;
  else
    printf("That is not a valid input.\n");
 }
  if(ans[0] == 'Y' || ans[0] == 'y')
   printf("I win!");
 else if(ans[0] == 'N' || ans[0] == 'n'){
   printf("What is your animal?\n");
   scanf("%s",ans);
   printf("Please enter a yes or no question that is true about %s?\n", ans);
   scanf("%s",q);
   node->right = newNode(q);
   node->right->left = newNode(ans);
   node->right->right = NULL;
  }
  writeAFile(root,f);
  fclose(f);
  return 0;
}

.h

#include<stdio.h>

struct Node {
 char *value;
 struct Node * left;
 struct Node * right;
};

struct Node * newNode (char *newValue) ;
char * newStr (char * charBuffer);
struct Node * readATree(FILE * f);
void writeAFile(struct Node* tree, FILE * f);
+3  A: 

Don't force the good people of SO to wade through and debug your code! Also, don't keep repeating your question. The reason it wasn't answered to your satisfaction earlier is that people were unwilling to compensate for your laziness.

That's what debuggers are for. Run your code in a debugger, and it will tell you when you're accessing a null pointer.

If you don't have a debugger, throw a bunch of print statements into your program. If you run your program, the last printout before the crash will be just above the place where your segmentation fault occurred. You may want to add even more print statements near that point, perhaps dumping out some pointer values.

Carl Smotricz
+3  A: 

From a cursory glance at the code:

node->right = NULL;
node->right->right=NULL;

The second line here will access a NULL pointer, which will cause a segfault.

In general, running the code in a debugger will let you see which line caused the error.

interjay
A: 

I'm guessing that these warnings are a clue:

animal.c: In function ‘main’:
animal.c:95: warning: format not a string literal and no format arguments
animal.c:96: warning: format not a string literal and no format arguments
msw