如何迭代将CSV文件行存入链表,处理带引号字段并完成节点链接?
CSV数据转链表的实现方案
核心需求
将CSV文件的每行数据存入链表,链表每个节点的data指针指向存储各列信息的结构体(如double heading1; char *heading2等),同时必须处理带引号的字段:引号内的逗号需作为字段的一部分,而非列分隔符(例如"1, 2, 3"要作为单个完整字段,不能拆成3列)。
原代码问题
你尝试用双层循环实现,但卡在了链表节点的迭代创建与链接上,同时fscanf本身不适合处理带引号的CSV字段,容易出现拆分错误。原代码片段:
// 定义数据结构 typedef struct node{ suburb_t *data; struct node *next; }Node_t; // 函数定义... while(fscanf("%d,%d,%d", &x, &y, &z) == 3){ Node_t node = (Node_t *)malloc(sizeof(Node_t)); // 这里类型错误,应该是Node_t* node->data = (suburb_t *)malloc(sizeof(suburb_t)); node->data->content1 = x; // ...其他字段赋值 while(fscanf(.....) == ...){ // 处理引号字段的逻辑 } // 不知道如何持续创建节点并链接 }
解决步骤
1. 修正链表节点的链接逻辑
要构建链表,需要维护两个指针:头节点(head)和尾节点(tail),每次创建新节点后:
- 如果链表为空(head为NULL),则head和tail都指向新节点;
- 如果链表非空,将tail的
next指向新节点,再把tail更新为新节点。
2. 实现带引号的CSV字段读取函数
fscanf无法处理引号内的逗号,因此需要自定义一个字段读取函数,逐字符处理:
- 遇到双引号时,读取直到下一个双引号结束;
- 未遇到双引号时,读取直到逗号或换行符结束。
3. 完整实现代码
假设suburb_t结构包含int id; char *name; double score;三个字段,完整代码如下:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> // 定义存储列信息的结构体 typedef struct { int id; char *name; double score; } suburb_t; // 链表节点结构体 typedef struct node { suburb_t *data; struct node *next; } Node_t; // 读取CSV的一个字段(处理带引号的情况) int read_csv_field(FILE *fp, char *buf, size_t buf_size) { int c; size_t idx = 0; // 跳过开头的空白字符 while ((c = fgetc(fp)) != EOF && isspace(c)); if (c == EOF) return 0; // 处理带引号的字段 if (c == '"') { while ((c = fgetc(fp)) != EOF && c != '"' && idx < buf_size - 1) { buf[idx++] = c; } // 跳过结束的引号 if (c == '"') fgetc(fp); } else { // 处理普通字段(直到逗号或换行) buf[idx++] = c; while ((c = fgetc(fp)) != EOF && c != ',' && c != '\n' && idx < buf_size - 1) { buf[idx++] = c; } } buf[idx] = '\0'; // 跳过字段后的逗号(如果有) if (c == ',') return 1; // 到换行或EOF,说明是该行最后一个字段 return (c == '\n' || c == EOF) ? 2 : 0; } // 创建新的链表节点 Node_t* create_node(suburb_t *data) { Node_t *node = malloc(sizeof(Node_t)); if (!node) return NULL; node->data = data; node->next = NULL; return node; } // 从CSV文件读取数据并构建链表 Node_t* build_linked_list(FILE *fp) { Node_t *head = NULL, *tail = NULL; char buf[256]; int res; while (1) { suburb_t *suburb = malloc(sizeof(suburb_t)); if (!suburb) break; // 读取第一个字段(id) res = read_csv_field(fp, buf, sizeof(buf)); if (res == 0) { free(suburb); break; } suburb->id = atoi(buf); // 读取第二个字段(name,可能带引号) res = read_csv_field(fp, buf, sizeof(buf)); if (res == 0) { free(suburb); break; } suburb->name = malloc(strlen(buf) + 1); strcpy(suburb->name, buf); // 读取第三个字段(score) res = read_csv_field(fp, buf, sizeof(buf)); if (res == 0 || res == 1) { free(suburb->name); free(suburb); break; } suburb->score = atof(buf); // 创建节点并链接到链表 Node_t *new_node = create_node(suburb); if (!new_node) { free(suburb->name); free(suburb); break; } if (!head) { head = new_node; tail = new_node; } else { tail->next = new_node; tail = new_node; } // 如果到文件末尾,退出循环 if (res == 2 && feof(fp)) break; } return head; } // 打印链表 void print_list(Node_t *head) { Node_t *current = head; while (current) { printf("ID: %d, Name: %s, Score: %.2f\n", current->data->id, current->data->name, current->data->score); current = current->next; } } // 释放链表内存 void free_list(Node_t *head) { Node_t *temp; while (head) { temp = head; head = head->next; free(temp->data->name); free(temp->data); free(temp); } } int main() { FILE *fp = fopen("test.csv", "r"); if (!fp) { perror("Failed to open file"); return 1; } Node_t *list = build_linked_list(fp); print_list(list); free_list(list); fclose(fp); return 0; }
代码说明
read_csv_field函数负责读取单个CSV字段,自动处理带引号的情况;build_linked_list函数中,每次读取一行的所有字段后创建节点,通过head和tail指针维护链表的链接;- 注意内存泄漏问题,所有malloc的内存都要在最后释放。
内容的提问来源于stack exchange,提问作者MZGhibli
相关产品推荐
相关产品推荐

