Reputation: 81
I have created a program in C where a CSV file consisting of coordinates and location IDs is inserted into a linked list. Now I want to create a separate function that prints out all location IDs where the x coordinate is equal to 20 or greater. However my program isn't printing out any values at all for some reason. How do I fix this?
#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("%lf\n",temp->id);
temp = temp->next;
}
}
}
Here is the CSV input file:
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
Upvotes: 1
Views: 63
Reputation: 81
temp = temp->next;
must be outside the if clause.
void search() {
struct wake *temp;
temp = head;
while(temp!=NULL) {
if (temp->x >= 20) {
printf("%lf\n",temp->id);
}
temp = temp->next;
}
}
Upvotes: 1