该代码旨在从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
创建一个称为new的指针时,malloc
函数中发生了棘手的事情,但是在调试时它不起作用,但是我认为它应该起作用! / p>
答案 0 :(得分:1)
当您分配新结构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);
}
请注意,如何在不使用运算符的情况下将一个字符串常量一个接一个地写成一个较小的块。编译将自动将这些字符串粘贴到单个文字中。
此外,更安全的方法是指定要存储到每个目标数组中的最大字符数,并验证是否正确解析了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;
}
还请注意,此基本解析器不处理引号字符串,带有嵌入式引号和/或逗号的字符串,也不接受空字段。需要手动编码的解析器才能正确读取文件。