无法查明分段错误

发布于 2024-09-12 08:05:26 字数 3261 浏览 2 评论 0原文

可能的重复:
知识树中的段错误

我无法在信息树中查明我的段错误。如果文件不存在,它应该从文件中读取或获取用户输入。它应该根据是或否的问题来猜测动物。我对 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);

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女情怀诗 2024-09-19 08:05:26

不要强迫 SO 的好人费力地检查和调试你的代码!另外,不要一直重复你的问题。之前之所以没有得到你满意的答复,是因为人们不愿意补偿你的懒惰。

这就是调试器的用途。在调试器中运行代码,它会告诉您何时访问空指针。

如果您没有调试器,请将一堆打印语句放入您的程序中。如果您运行程序,崩溃前的最后一个打印输出将位于发生分段错误的位置的正上方。您可能想在该点附近添加更多打印语句,也许转储一些指针值。

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.

清风疏影 2024-09-19 08:05:26

粗略地看一下代码:

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

这里的第二行将访问一个NULL指针,这将导致段错误。

一般来说,在调试器中运行代码可以让您看到哪一行导致了错误。

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.

望笑 2024-09-19 08:05:26

我猜这些警告是一条线索:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文