gpt4 book ai didi

c - SOS 我疯了,但还没找到 137 线不起作用的原因

转载 作者:行者123 更新时间:2023-11-30 21:36:26 24 4
gpt4 key购买 nike

该代码旨在从 CSV 文件(以逗号分隔)中读取每一行,然后将每一行作为值插入二叉树中。

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

#define MAX_CHAR 128
#define MAX_LINE 512
#define COMMA ","

typedef struct node node_t;
typedef struct {
node_t *root;
int (*cmp)(void *, void *);
} tree_t;

typedef struct {
char id[MAX_CHAR];
char sex[MAX_CHAR];
char age[MAX_CHAR];
char height[MAX_CHAR];
char weight[MAX_CHAR];
char team[MAX_CHAR];
char noc[MAX_CHAR];
char games[MAX_CHAR];
char year[MAX_CHAR];
char season[MAX_CHAR];
char city[MAX_CHAR];
char sport[MAX_CHAR];
char event[MAX_CHAR];
char medal[MAX_CHAR];
} data_t;

typedef struct {
char *name;
data_t data;
} athlete_t;

struct node {
athlete_t *data;
node_t *left;
node_t *right;
};

tree_t *make_empty_tree(int func(void *, void *));
void insert_to_struct(athlete_t *, char *);
tree_t *read_file(char*, tree_t *);
int cmp(void *p1, void *p2);
tree_t *insert_in_order(tree_t *, athlete_t *);
node_t *recursive_insert(node_t *, node_t *, int cmp(void *, void *));
void traverse_tree(tree_t *, void action(void *));
void recursive_traverse(node_t *, void action(void *));
void action(void *);
void ini_struct(athlete_t *);

int main(int argc, char **argv) {
tree_t *tree = make_empty_tree(cmp);
tree = read_file(argv[1], tree);
printf("%s\n", tree->root->data->name);
traverse_tree(tree, action);
return 0;
}

/* Read each line from a file, and assume max length of line is 512 chars*/
tree_t *read_file(char *filename, tree_t *tree) {
FILE *fp_data;
char new_line[MAX_LINE];
athlete_t *data_struct;

fp_data = fopen(filename, "r");
if (fp_data == NULL) {
fprintf(stderr, "Cannot open %s\n", filename);
exit(EXIT_FAILURE);
}
while (fgets(new_line, MAX_LINE, fp_data) != NULL) {
data_struct = (athlete_t *)malloc(sizeof(*data_struct));
ini_struct(data_struct);
insert_to_struct(data_struct, new_line);
tree = insert_in_order(tree, data_struct);
printf("%s\n", new_line);
}
printf("%s \n", new_line);
fclose(fp_data);
return tree;
}

/* initialize the struct */
void ini_struct(athlete_t *data_struct) {
strcpy(data_struct->data.id, "");
strcpy(data_struct->name, "");
strcpy(data_struct->data.sex, "");
strcpy(data_struct->data.age, "");
strcpy(data_struct->data.height, "");
strcpy(data_struct->data.weight, "");
strcpy(data_struct->data.team, "");
strcpy(data_struct->data.noc, "");
strcpy(data_struct->data.games, "");
strcpy(data_struct->data.year, "");
strcpy(data_struct->data.season, "");
strcpy(data_struct->data.city, "");
strcpy(data_struct->data.sport, "");
strcpy(data_struct->data.event, "");
strcpy(data_struct->data.medal, "");
}

/* make a empty tree */
tree_t *make_empty_tree(int func(void *, void *)) {
tree_t *tree;
tree = (tree_t *)malloc(sizeof(*tree));
assert(tree != NULL);
tree->root = NULL;
tree->cmp = func;
return tree;
}

/* insert each value separated by comma into the struct we define */
void insert_to_struct(athlete_t *data_struct, char *new_line) {
sscanf(new_line, \
"%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,]", \
data_struct->data.id, data_struct->name, data_struct->data.sex, \
data_struct->data.age, data_struct->data.height,\
data_struct->data.weight,data_struct->data.team, \
data_struct->data.noc, data_struct->data.games, \
data_struct->data.year, data_struct->data.season, \
data_struct->data.city, data_struct->data.sport, \
data_struct->data.event, data_struct->data.medal);
}

int cmp(void *p1, void *p2) {
char *s1 = (char *)p1;
char *s2 = (char *)p2;

return strcmp(s1, s2);
}

/* insert the struct into the tree */
tree_t *insert_in_order(tree_t *tree, athlete_t *value) {
node_t *new;
new = malloc(sizeof(node_t));
assert(new != NULL);
new->data = value;
new->left = new->right = NULL;
tree->root = recursive_insert(tree->root, new, tree->cmp);
return tree;
}

node_t *recursive_insert(node_t *root, node_t *new, int cmp(void*, void*)) {
if (root == NULL) {
return new;
} else if (cmp(new->data->name, root->data->name) <= 0) {
root->left = recursive_insert(root->left, new, cmp);
} else {
root->right = recursive_insert(root->right, new, cmp);
}
return root;
}

void recursive_traverse(node_t *root, void action(void *)) {
if (root != NULL) {
recursive_traverse(root->left, action);
action(root->data->name);
recursive_traverse(root->right, action);
}
}

void traverse_tree(tree_t *tree, void action(void *)) {
assert(tree != NULL);
recursive_traverse(tree->root, action);
}

void action(void *p1) {
printf("%s\n", (char*)p1);
}

棘手的事情发生在 make_empty_tree 函数中,当我使用 malloc 创建一个名为 new 的指针时,但在调试时它不起作用,但我认为它应该有效!

最佳答案

当你分配一个新的结构node_t时,它的成员name是一个未初始化的指针,不能通过ini_struct()初始化>strcpy(data_struct->name, ""); 这具有未定义的行为,并且在 insert_to_struct() 中也出现同样的问题。一个简单的解决方案是将 name 定义为 char 数组,就像 data 的成员一样:

typedef struct {
char name[MAX_STR];
data_t data;
} athlete_t;

另请注意,函数 insert_to_struct 不需要行继续符 \。它将更具可读性:

/* insert each value separated by comma into the struct we define */
void insert_to_struct(athlete_t *data_struct, char *new_line) {
sscanf(new_line,
"%[^,],%[^,],%[^,],%[^,],%[^,],"
"%[^,],%[^,],%[^,],%[^,],%[^,],"
"%[^,],%[^,],%[^,],%[^,],%[^,]",
data_struct->data.id, data_struct->name,
data_struct->data.sex,
data_struct->data.age, data_struct->data.height,
data_struct->data.weight,data_struct->data.team,
data_struct->data.noc, data_struct->data.games,
data_struct->data.year, data_struct->data.season,
data_struct->data.city, data_struct->data.sport,
data_struct->data.event, data_struct->data.medal);
}

请注意,如何通过在不使用运算符的情况下将字符串常量一个接一个地写入来将字符串常量分解为更小的 block 。编译会自动将这些字符串粘合成单个文字。

此外,指定要存储到每个目标数组中的最大字符数并验证 15 个字段是否已正确解析会更安全:

/* insert each value separated by comma into the struct we define */
int insert_to_struct(athlete_t *data_struct, char *new_line) {
return sscanf(new_line,
"%127[^,],%127[^,],%127[^,],%127[^,],%127[^,],"
"%127[^,],%127[^,],%127[^,],%127[^,],%127[^,],"
"%127[^,],%127[^,],%127[^,],%127[^,],%127[^,]",
data_struct->data.id, data_struct->name,
data_struct->data.sex,
data_struct->data.age, data_struct->data.height,
data_struct->data.weight,data_struct->data.team,
data_struct->data.noc, data_struct->data.games,
data_struct->data.year, data_struct->data.season,
data_struct->data.city, data_struct->data.sport,
data_struct->data.event, data_struct->data.medal) == 15;
}

另请注意,这个基本解析器不处理引号字符串、嵌入引号和/或逗号的字符串,也不接受空字段。需要手动编码的解析器才能正确读取文件。

关于c - SOS 我疯了,但还没找到 137 线不起作用的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52079579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com