知识树中的段错误

编程入门 行业动态 更新时间:2024-10-19 06:20:31
本文介绍了知识树中的段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在用 c 语言实现一个可以从文件中读取的知识树.我的 newStr 函数出现段错误.我无法用这个问题测试我的其余代码.我对c没有太多经验.任何帮助将不胜感激.

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.

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

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 + ""); } /*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. "); } 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? ", ans); scanf(q); node->right = newNode(q); node->right->left = newNode(ans); node->right->right = NULL; } writeAFile(root,f); fclose(f); return 0; }

.h 文件#include

.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:

  • 你的 newStr 函数非常,非常错误.估计你会想要类似:

  • 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; }

  • 不能将字符串转换为字符就像你在这里做的一样:

  • 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')

  • 和上面调用putc时一样,不要做

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

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

    fputc('A', f);

  • scanf 需要格式字符串,不要做:

  • scanf needs a format string, don't do:

    scanf(ans);

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

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

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

  • 更多推荐

    知识树中的段错误

    本文发布于:2023-10-09 05:56:57,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1474895.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:错误   知识

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!