hello guys i tried to read file into linked list and then print it
but when i print it the first sentences is deleted .
what am i doing wrong?
Code:
int read_file(char * filename) { int i = 0;
int a[20], b[20];
FILE *fp;
char line[128];
LIST *current, *head;
head = current = NULL;
fopen_s(&fp, filename, "r");
if (fp == NULL)
return 0;
while (fgets(line, sizeof(line), fp)) {
LIST *node = malloc(sizeof(LIST));
node->string = strdup(line);
node->next = NULL;
if (head == NULL) {
current = head = node;
}
else {
current = current->next = node;
char str[20];
char* field = strtok(line, ",");
strcpy(str, field);
a[i] = (double)atof(str);
field = strtok(NULL, ",");
b[i] = (double)atof(field);
i++;
}
}
fclose(fp);
printf("Thermic table\nx y\n====== ======\n");
for (int k = 0; k < 13; k++)
printf("%d %d\n", a[k], b[k]);
printf("\n--------------------------\nLets fly\n--------------------------");
free_list(head);
return 1;
my output:15000 8500
1000 10000
19000 20100
35000 40100
23000 30000
25000 42000
300 19000
3000 30000
6000 25000
6000 40000
15000 43000
30000 15555
the real output :
5700,5000
15000,8500
1000,10000
19000,20100
35000,40100
23000,30000
25000,42000
300,19000
3000,30000
6000,25000
6000,40000
15000,43000
30000,15555