我在C中创建了一个程序,其中将一个由坐标和位置ID组成的CSV文件插入到链接列表中。现在我想创建一个单独的函数,打印出x坐标等于20或更大的所有位置ID。然而,由于某种原因,我的程序根本没有打印出任何值。我该如何解决这个问题?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define MAX 10
struct data {
double x, y, id;
struct data *next;
}*head;
typedef struct data data;
void read_csv();
void search();
int main(int argc, char *argv[]) {
read_csv();
}
void read_csv() {
// Opens the CSV datafile
FILE *fp = fopen("data.csv", "r");
char buffer[MAX];
struct data** tail = &head;
while (fgets(buffer, MAX, fp)) {
data *node = malloc(sizeof(data));
node->x = atof(strtok(buffer, ","));
node->y = atof(strtok(NULL, ","));
node->id = atof(strtok(NULL, ","));
node->next = NULL;
*tail = node;
tail = &node->next;
}
search();
}
void search() {
struct data *temp;
temp = head;
while(temp!=NULL) {
if (temp->x >= 20) {
printf("%lfn",temp->id);
temp = temp->next;
}
}
}
这是CSV输入文件:
19.743748,-11.838155,0.947989
19.810734,-11.838155,0.947972
19.877850,-11.838155,0.947953
19.945097,-11.838155,0.947930
20.012476,-11.838155,0.947904
20.079988,-11.838155,0.947875
20.147631,-11.838155,0.947844
20.215405,-11.838155,0.947812
20.283312,-11.838155,0.947780
temp = temp->next;
必须在if子句之外。
void search() {
struct wake *temp;
temp = head;
while(temp!=NULL) {
if (temp->x >= 20) {
printf("%lfn",temp->id);
}
temp = temp->next;
}
}