赛格故障的知识树 [英] Seg Fault in Knowledge Tree

查看:104
本文介绍了赛格故障的知识树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现在C知识树,可以从文件中读取。我得到我的函数中newstr一个赛格故障。我不能用这个问题来测试我的code的其余部分。我没有与C太多经验。任何帮助将大大AP preciated。

我的.c文件
        #包括
        #包括
        #包括animal.h
        #包括
        的#include

  / *返回一个新的节点给定值* /
结构节点* newNode(字符* newValue)以
{
结构节点*树;
树=(结构节点*)malloc的(的sizeof(结构节点));
树 - >值=中newstr(为newValue);
返回树;
}
/ *返回作为参数传递值一个新的字符串* /
字符*中newstr(字符* CharBuffer的)
{
INT I;
INT长度= strlen的(CharBuffer的);
焦炭中newstr;
如果(CharBuffer的[0] =='A'|| CharBuffer的[0] =='Q'){
    对于(i = 1; I<长度;我+ +)
        +中newstr CharBuffer的= [I]
}
回报(+中newstr\\ 0);
}/ *从文件读取并创建树* /
结构节点* readATree(FILE * F)
{
  焦炭℃;
  炭缓冲器[100];
  结构节点* NEWTREE;
  C =龟etc(F);
  如果(C =='A'){
     与fgets(缓冲,100,F);
     友川= newNode(缓冲);
     友川 - >左= NULL;
     友川 - >右= NULL;
    }
  其他{
     与fgets(缓冲,100,F);
     NEWTREE = newNode(中newstr(缓冲液));
     newTree->左= readATree(F);
     newTree->右=(结构节点*)readAtree(F);
     }
  返回NEWTREE;}/ *写树到一个文件* /
无效writeAFile(结构节点*树,FILE * F)
{
    炭缓冲器[100];
    的strcpy(缓冲区,树形>值);
    如果(树!= 0){
        如果(树形>左== NULL和放大器;&安培;树形>右== NULL){
            的fputc((char)的A,F);
            的fputs(缓冲,F);
        }其他{
            的fputc((char)的Q,F);
            的fputs(缓冲,F);
            writeAFile(树形>左,F);
            writeAFile(树形>右,F);
        }
    }
}/ *该剧应该从这里开始* /
诠释主(){
    结构节点*节点;
    结构节点*根;
    炭ANS [100];
    焦炭Q [100];
    FILE * F;
    F =的fopen(animal.txt,R +);
    如果(F!= NULL)
        readATree(F);
    其他{
        节点= newNode(它喵?);
    与于节点GT;右= NULL;
    与于节点的GT;右>右= NULL;
    与于节点GT;左>左= NULL;
    与于节点GT;左= newNode(猫);
    根=节点;
}
而(与于节点GT;左= NULL&放大器;!&安培;&于节点GT;!右= NULL){
    的printf(与于节点GT;值);
    scanf的(ANS);
    如果(ANS [0] ==(char)的Y|| ANS [0] ==(char)的Y)
        节点=&于节点GT;左;
    否则,如果(ANS [0] ==(char)的N|| ANS [0] ==(char)的N)
        节点=&于节点GT;的权利;
    其他
        的printf(这不是一个有效的输入\\ n);
}
如果(ANS [0] ==(char)的Y|| ANS [0] ==(char)的Y)
    的printf(我赢了!);
否则,如果(ANS [0] ==(char)的N|| ANS [0] ==(char)的N){
    的printf(你是什么动物);
    scanf的(ANS);
    的printf(请输入yes或no的问题即大约%的真实\\ n吗?,ANS);
    scanf的(Q);
    与于节点GT;右= newNode(Q);
    与于节点的GT;右>左= newNode(ANS);
    与于节点的GT;右>右= NULL;
}
writeAFile(根,F);
FCLOSE(F);
返回0;
}

.h文件中
    的#include

 结构节点{
字符*值;
结构节点*离开;
结构节点*权利;
};结构节点* newNode(字符*为newValue);
字符*中newstr(字符* CharBuffer的);
结构节点* readATree(FILE * F);
无效writeAFile(结构节点*树,FILE * F);


解决方案

有可能有几个,但这里是什么地方不对劲几点:


  1. 您函数中newstr只是非常,
    非常错误的。在猜测你想要
    是这样的:

     的char *中newstr(字符* CharBuffer的)
    {
      字符*中newstr;
      如果(CharBuffer的[0] =='A'|| CharBuffer的[0] =='Q'){
        中newstr =的strdup(安培; CharBuffer的[1]);
      }其他{
        中newstr =的strdup();
      }
      如果(中newstr == NULL){
          //处理错误
      }
      返回中newstr;
    }


  2. 您可以不投一个字符串为char
    就像你在这里做的:

     如果(ANS [0] ==(char)的Y|| ANS [0] ==(char)的Y)

    而不是做(同为类似code
    其他地方也行)

     如果(ANS [0] =='Y'|| ANS [0] ==Y)


  3. 一样的,当你调用putc将上面,
    不做

     的fputc((char)的A,F);

    不要

     的fputc('A',F);


  4. scanf函数需要一个格式字符串,不
    这样做:

      scanf函数(ANS);

    例如做(或者只是再次使用与fgets)

     如果(scanf函数(%99秒,ANS)!= 1){
       //处理错误
     }


I am implementing a knowledge tree in c that can read from a file. I am getting a seg fault in my newStr function. I'm not able to test the rest of my code with this problem. I don't have much experience with c. Any help would be greatly appreciated.

my .c file #include #include #include"animal.h" #include #include

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


/* returns a new string with value passed as an argument*/
char * newStr (char * charBuffer)
{
int i;
int length = strlen(charBuffer);
char newStr;
if(charBuffer[0] == 'A' || charBuffer[0] == 'Q'){
    for(i=1; i<length; i++)
        newStr += charBuffer[i]; 
}
return (newStr + "\0");
}

/*Read from a File and create a tree*/
struct Node * readATree(FILE * f)
{
  char c;
  char buffer[100];
  struct Node * newTree;
  c = fgetc(f);
  if (c == 'A'){
     fgets(buffer, 100, f);
     newTree = newNode(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)
{
    char buffer[100];
    strcpy(buffer, tree->value);
    if(tree != 0){
        if(tree->left == NULL && tree->right == NULL){
            fputc((char)"A", f);
            fputs(buffer,f);
        } else{
            fputc((char)"Q",f);
            fputs(buffer,f);
            writeAFile(tree->left, f);
            writeAFile(tree->right,f);
        }
    }
}

/*The play should start from here*/
int main (){
    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] == (char)"Y" || ans[0] == (char)"y")
        node = node->left;
    else if(ans[0] == (char)"N" || ans[0] == (char)"n")
        node = node->right;
    else
        printf("That is not a valid input.\n");
}
if(ans[0] == (char)"Y" || ans[0] == (char)"y")
    printf("I win!");
else if(ans[0] == (char)"N" || ans[0] == (char)"n"){
    printf("What is your animal");
    scanf(ans);
    printf("Please enter a yes or no question that is true about %s?\n", ans);
    scanf(q);
    node->right = newNode(q);
    node->right->left = newNode(ans);
    node->right->right = NULL;
}
writeAFile(root,f);
fclose(f);
return 0;
}

.h file #include

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

解决方案

There might be several more, but here's some points on what's wrong:

  1. Your newStr function is just very, very wrong. At a guess you'd want something like:

    char * newStr (char * charBuffer)
    {
      char *newStr;
      if(charBuffer[0] == 'A' || charBuffer[0] == 'Q') {
        newStr = strdup(&charBuffer[1]);
      } else {
        newStr = strdup("");
      }
      if(newStr == NULL) {
          //handle error
      }
      return newStr;
    }
    

  2. You can't cast a string to a char like you do here:

     if(ans[0] == (char)"Y" || ans[0] == (char)"y")
    

    Do instead(same for similar code elsewhere too)

     if(ans[0] =='Y' || ans[0] == 'y')
    

  3. Same as above when you call putc, don't do

     fputc((char)"A", f);
    

    Do

     fputc('A', f);
    

  4. scanf needs a format string, don't do:

    scanf(ans);
    

    Do e.g. (or just use fgets again)

    if(scanf("%99s",ans) != 1) {
       //handle error
     }
    

这篇关于赛格故障的知识树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆