Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 解析读入文件并存储在b树中_C_Parsing_Segmentation Fault_Binary Tree - Fatal编程技术网

C 解析读入文件并存储在b树中

C 解析读入文件并存储在b树中,c,parsing,segmentation-fault,binary-tree,C,Parsing,Segmentation Fault,Binary Tree,我很确定我的程序会永远运行,因为当我运行程序时,不会产生任何输出,甚至不会产生分段错误。基本上,我想让我的程序读取一个4行的文件。然后,我们使用用户创建的标记器函数和已经创建的strtok函数解析字符串。然后我想用读入的命令生成一个二叉树。就像我前面说的,我的程序当时不产生任何输出,但它可以编译。我将发布我拥有的代码和您读入的文件以及示例输出。我感谢你的帮助 #include<stdlib.h> #include<string.h> #include<stdio.h

我很确定我的程序会永远运行,因为当我运行程序时,不会产生任何输出,甚至不会产生分段错误。基本上,我想让我的程序读取一个4行的文件。然后,我们使用用户创建的标记器函数和已经创建的strtok函数解析字符串。然后我想用读入的命令生成一个二叉树。就像我前面说的,我的程序当时不产生任何输出,但它可以编译。我将发布我拥有的代码和您读入的文件以及示例输出。我感谢你的帮助

#include<stdlib.h>
#include<string.h>
#include<stdio.h>

#define COMMAND_NAME_LEN 50
#define MAX_SPLIT_SIZE 50
#define MAX_BUFF_SIZE 50

typedef struct Command_ {
    char name[COMMAND_NAME_LEN];
    int expected_param_count;
    struct Command_ *left;
    struct Command_ *right;
}Command;


typedef struct StringArray_ {
    char **strings;
    int size;
}StringArray;

StringArray* tokenizer (char *string, const char* delimiters);
void free_string_array(StringArray *sr);
void create_commands_tree(Command **commands, const char *file);
void insert_into_commands_tree(Command** node, char** data);
Command* get_command(Command *node, const char *command);
Command* create_command(char **data);
void destroy_commands_tree(Command* node);
void display_commands(Command *node);


int main (int argc, char *argv[]) {

    if (argc < 2) {
            printf("%s is missing commands.dat\n", argv[0]);
            return 0;
    }


    Command* options = NULL;
    create_commands_tree(&options,argv[1]);
    int checking = 1;

    char input_buffer[MAX_BUFF_SIZE];

    do {
            printf("Command: ");
            fgets(input_buffer,MAX_BUFF_SIZE,stdin);
            StringArray* parsed_input = tokenizer(input_buffer," \n");
            Command* c = get_command(options,parsed_input->strings[0]);

            if( c && parsed_input->size == c->expected_param_count) {
                    if (strcmp(c->name, "quit") == 0){
                                    checking = 0;
                    }
                    printf("Valid command used\n");
            }
            else {
                    printf("Invalid command, please try again\n");
            }
            free_string_array(parsed_input);

    }while (checking);

    destroy_commands_tree(options);

}


void create_commands_tree(Command **commands, const char *file) {

    FILE *input;
    input = fopen(file, "r");
    char strings[256];
    Command *temp;
    StringArray *temp2;

    while(fgets(strings,100,input)){

            temp2 = tokenizer(strings, "\n");
            insert_into_commands_tree(&temp,temp2->strings);
    }
}

void insert_into_commands_tree(Command** node, char** data) {

    Command **new_ = node;

    if(node == NULL){
            *new_ = create_command(data);
    }
    else if( new_ != NULL){
            if(strcmp(data[0],(*new_)->name) < 0)
                    insert_into_commands_tree(&(*new_)->left,data);
            else if(strcmp(data[0], (*new_)->name) > 0)
                    insert_into_commands_tree(&(*new_)->right,data);
    }


}

Command* create_command(char **data) {

    Command* new_;
    new_ = (Command*)malloc(sizeof(Command));
    strncpy(new_->name, data[0], COMMAND_NAME_LEN);
    new_->expected_param_count = 0;
    new_->right = NULL;
    new_->left = NULL;


    return new_;

}

Command* get_command(Command *node, const char *command) {

    Command *temp = node;
    int compare;

    if(temp){
            compare = strcmp(node->name, command);
            if(compare == 0){
                    return temp;
            }
            else if(compare < 0){
                    return (get_command(node->right, command));
            }
            else{
                    if(compare > 0){
                            return (get_command(node->left, command));
            }}

    }
   return temp;
}

void destroy_commands_tree(Command* node) {

    if( node == NULL){
            return;
            }

    destroy_commands_tree(node->left);
    destroy_commands_tree(node->right);
    free(node);

}
void display_commands(Command *node) {

      if(node != NULL){
            printf("\npickup <item>");
            printf("\nhelp ");
            printf("\nquit ");
            printf("\nload <file>\n\n");
}
}
 StringArray* tokenizer (char *string, const char* delimiters){

    StringArray *temp = (StringArray*)malloc(sizeof(StringArray));;
    char *split;

    split = strtok(string, delimiters);

    while(split != NULL)
    {
            split = strtok(string, delimiters);
            temp->strings = &split;
    }
    return temp;
}

void free_string_array(StringArray *sr) {

    while(sr != NULL)
            free(sr);

    free(sr);

}
我们读到的文件如下:

pickup,2
help,1
quit,1
load,2

从strtok手册页:

   The strtok() function breaks a string into a sequence of zero or more nonempty tokens.  On the
   first call to strtok() the string to be parsed should be specified in str.  In each subsequent
   call that should parse the same string, str must be NULL.

您的标记器未正确使用strtok。你的代码被困在while循环中。

你可能会考虑尝试改进你原来的问题,而不是试图重新发布它。没有人回应我的旧的,有人回应了这个,并帮助我,所以我说它是值得的,而你可能觉得这是一个好主意这次,这不是一个好的模式落入。你可以通读一遍,寻找合适的方法来吸引大家对你的问题的注意。最好的方法是首先问一个好问题。在帮助中心有很多关于这方面的建议。非常感谢你的帮助
   The strtok() function breaks a string into a sequence of zero or more nonempty tokens.  On the
   first call to strtok() the string to be parsed should be specified in str.  In each subsequent
   call that should parse the same string, str must be NULL.