c - 读取文件到链表 C
问题描述
大家好,我正在尝试将文件加载到链表,然后将其打印到 2 个数组。我不知道我的代码有什么问题,我不知道如何处理 2 个数组,当我将它打印为字符串时它可以工作,但对于两个数组它不起作用。我是 c 编程的新手,我的代码:
struct list {
char *string1;
struct list *next;
};
typedef struct list LIST;
int read_file(char * filename)
int a[20], b[20];
FILE *fp;
char line[128];
LIST *current, *head;
head = current = NULL;
int i = 0;
fopen_s(&fp, filename, "r");
if (fp == NULL) {
printf("ERROR");
return 0;
}
while (fgets(line, sizeof(line), fp)) {
LIST *node = malloc(sizeof(LIST));
node->string1 = strdup(line);
node->next = NULL;
char str[20];
char* field = strtok(line, ",");
strcpy(str, field);
a[i] = (int)atof(str);
field = strtok(NULL, ",");
b[i] = (int)atof(field);
i++;
printf("%4d\t%4d\n", a[i],b[i]);
if (head == NULL) {
current = head = node;
}
else {
current = current->next = node;
}
}
fclose(fp);
解决方案
推荐阅读
- swift - 根据条件将对象从一个数组添加到另一个数组
- javascript - div调整大小时如何停止点击计数
- npm - 如何使用“显示所有命令”在 VScode 中运行 npm 脚本,而不是单击侧边栏?
- python-3.x - 无法使用 pip 安装
- python - 如何使用最新的所有历史数据滚动应用函数?
- javascript - Vue:两步绘制元素
- python - Python Django psycopg2.errors.StringDataRightTruncation:类型字符的值太长
- hazelcast - 如何使 Hazelcast MapStore 在条目驱逐事件中删除持久层中的条目
- javascript - Javascript 按钮 onClick 事件
- linux - 使用 bash 在类似字典的格式中进行迭代