2010-07-30 53 views
-3

可能重複:
Seg Fault in Knowledge Tree無法查明分段故障

我無法找出我的賽格故障在我的信息樹。如果文件不存在,它應該從文件讀取或獲取用戶輸入。它應該根據是或否的問題來猜測動物。我對c有限的經驗,所以任何幫助將不勝感激。

.c文件

#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); 
+0

看起來很熟悉:http://stackoverflow.com/questions/3366760/seg-fault-in-knowledge-tree和http:// stackoverflow.com/questions/3356505/remove-first-char-of-string-c – 2010-07-30 15:54:47

+0

在調試器下運行你的程序。當它碰到段錯誤時,看看回溯。 – nmichaels 2010-07-30 16:11:07

回答

3

不要強迫的好心人通過SO和調試涉水你的代碼!另外,不要繼續重複你的問題。之前沒有回答你的滿意的原因是人們不願意彌補你的懶惰。

這就是調試器的用途。在調試器中運行你的代碼,它會告訴你什麼時候訪問一個空指針。

如果您沒有調試器,請將一堆打印語句放入您的程序中。如果您運行程序,崩潰前的最後一個打印輸出將位於發生分段錯誤的位置之上。您可能希望在該點附近添加更多的打印語句,可能會拋出一些指針值。

3

從在代碼粗略地看一眼:

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

第二行這裏將訪問一個NULL指針,這將導致一個段錯誤。

通常,在調試器中運行代碼可以讓您看到哪條線路導致錯誤。

0

我猜測,這些警告是一個線索:

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