C SOS我疯了,但我没有';我没有找到第137行没有';行不通

C SOS我疯了,但我没有';我没有找到第137行没有';行不通,c,csv,C,Csv,该代码旨在从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

该代码旨在从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);
}
#包括
#包括
#包括
#包括
#定义最大字符128
#定义MAX_行512
#定义逗号“,”
类型定义结构节点;
类型定义结构{
节点_t*根;
int(*cmp)(无效*,无效*);
}树木;
类型定义结构{
字符id[MAX_char];
字符性别[最大字符];
字符年龄[MAX_char];
字符高度[最大字符];
煤焦重量[最大煤焦];
char团队[MAX_char];
字符noc[MAX_char];
char游戏[MAX_char];
字符年[最大字符];
焦炉季节[最大焦炉];
查尔城[MAX_char];
char sport[MAX_char];
字符事件[最大字符];
查尔奖章[马克斯·查尔];
}数据;
类型定义结构{
字符*名称;
数据;
}运动员;;
结构节点{
运动员数据;
节点_t*左;
节点_t*右;
};
树(int-func(void*,void*));
无效插入结构(运动员,字符*);
tree_t*读取文件(char*,tree_t*);
int cmp(无效*p1,无效*p2);
树*按顺序插入(树*运动员*);
node_t*递归插入(node_t*,node_t*,int-cmp(void*,void*);
无效遍历树(树t*,无效操作(无效*);
无效递归遍历(节点t*,无效操作(无效*);
无效诉讼(无效*);
无效初始结构(运动员*);
int main(int argc,字符**argv){
tree\u t*tree=使树为空(cmp);
tree=read_文件(argv[1],tree);
printf(“%s\n”,树->根->数据->名称);
遍历树(树,动作);
返回0;
}
/*从文件中读取每一行,并假设行的最大长度为512个字符*/
树*读取文件(字符*文件名,树*树){
文件*fp_数据;
新字符行[最大字符行];
运动员数据结构;
fp_data=fopen(文件名,“r”);
if(fp_data==NULL){
fprintf(stderr,“无法打开%s\n”,文件名);
退出(退出失败);
}
while(fgets(新建线、最大线、fp线数据)!=NULL){
数据结构=(运动员*)malloc(sizeof(*数据结构));
ini结构(数据结构);
插入到结构(数据结构,新行);
树=按顺序插入(树,数据结构);
printf(“%s\n”,新行);
}
printf(“%s\n”,新行);
fclose(fp_数据);
回归树;
}
/*初始化结构*/
无效ini结构(运动员*数据结构){
strcpy(data_struct->data.id,“”);
strcpy(数据结构->名称“”;
strcpy(数据结构->data.sex,”;
strcpy(data_struct->data.age,“”);
strcpy(数据结构->数据高度“”;
strcpy(data_struct->data.weight,“”);
strcpy(数据结构->data.team,“”);
strcpy(数据结构->data.noc,”;
strcpy(数据结构->data.games,”;
strcpy(数据结构->data.year,“”);
strcpy(数据结构->data.seasure,”;
strcpy(data_struct->data.city,“”);
strcpy(数据结构->data.sport,”;
strcpy(data_struct->data.event,“”);
strcpy(数据结构->数据结构“”;
}
/*造一棵空树*/
树(int func(void*,void*)){
tree_t*tree;
tree=(tree_t*)malloc(sizeof(*tree));
断言(树!=NULL);
树->根=NULL;
树->cmp=func;
回归树;
}
/*将逗号分隔的每个值插入到我们定义的结构中*/
无效插入到结构(运动员*数据结构,字符*新行){
sscanf(新线)\
"%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,]", \
data\u struct->data.id,data\u struct->name,data\u struct->data.sex\
数据结构->数据年龄,数据结构->数据高度\
数据结构->数据重量,数据结构->数据团队\
数据结构->数据.noc,数据结构->数据.games\
数据结构->数据年,数据结构->数据季节\
数据结构->数据城市,数据结构->数据体育\
数据结构->数据事件,数据结构->数据奖牌);
}
int cmp(无效*p1,无效*p2){
char*s1=(char*)p1;
char*s2=(char*)p2;
返回strcmp(s1,s2);
}
/*将结构插入到树中*/
树*按顺序插入(树*树,运动员*值){
节点_t*新建;
新=malloc(sizeof(node_t));
断言(新!=NULL);
新建->数据=值;
新建->左=新建->右=空;
tree->root=递归插入(tree->root,new,tree->cmp);
回归树;
}
node_t*递归插入(node_t*根,node_t*新,int-cmp(void*,void*)){
if(root==NULL){
归还新的;
}else if(cmp(新建->数据->名称,根->数据->名称)left=recursive_insert(根->左侧,新建,cmp);
}否则{
root->right=递归插入(root->right,new,cmp);
}
返回根;
}
无效递归遍历(节点*根,无效操作(无效*){
if(root!=NULL){
递归遍历(根->左,动作);
操作(根->数据->名称);
递归遍历(根->右,操作);
}
}
无效遍历树(树,无效操作(无效*){
断言(树!=NULL);
递归遍历(树->根,操作);
}
无效操作(无效*p1){
printf(“%s\n”,(char*)p1);
}

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

当分配一个新结构
节点时,它的成员
name
是一个无法初始化的未初始化指针通过
ini\u struct()
strcpy(data\u struct->name,”)
这有未定义的行为,在
insert\u to\u struct()
中也会出现同样的问题。一个简单的解决方案是将
name
定义为
字符的数组,就像
data
的成员一样:

typedef struct {
    char name[MAX_STR];
    data_t data;
} athlete_t;
还请注意,函数
插入到结构中
不需要行连续字符
\
。它的可读性更好,因为:

/* 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);
}
注意一个字符串常量是如何通过在t之后一个一个地写入而被分解成更小的块的
/* 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;
}