I am trying to take the data from the CSV file output.csv using strtok and then print it out. However, when I print it out, it seems to split the last datapoints into 2 entries, with the second one being put in the next entry in the array.
time,identifier,x,y,z
2000.123,A123,2.849,0.34,-0.543
2001.456,A123,3.493,0.19,-0.987
2001.735,A123,5.403,1.587,0.579,-0.254
I'm reading the data and using a struct and an array to store it.
struct Data {
double Time_Data;
char* ID;
double X_Data;
double Y_Data;
double Z_Data;
};
int main(int argc, char *argv[]){
char* field;
char line[50];
struct Data dataPoints[3567];
FILE * pointer;
fpointer = fopen("output.csv", "r");
if(fpointer == NULL){
printf("Unable to open file \n");
return 0;
}
int LineCount = 0;
while(fgets(line, 30, fpointer) != NULL){
field = strtok(line, ",");
dataPoints[LineCount].Time_Data = atof(field);
field = strtok(NULL, ",");
field = strtok(NULL, ",");
dataPoints[LineCount].X_Data = atof(field);
field = strtok(NULL, ",");
dataPoints[LineCount].Y_Data = atof(field);
field = strtok(NULL, ",");
dataPoints[LineCount].Z_Data = atof(field);
LineCount++;
}
fclose(fpointer);
printf("%lf %lf %lf %lf\n", dataPoints[0].Time_Data,
dataPoints[0].X_Data,dataPoints[0].Y_Data, dataPoints[0].Z_Data);
printf("%lf %lf %lf %lf\n", dataPoints[1].Time_Data,
dataPoints[1].X_Data,dataPoints[1].Y_Data, dataPoints[1].Z_Data);
printf("%lf %lf %lf %lf\n", dataPoints[2].Time_Data,
dataPoints[2].X_Data,dataPoints[2].Y_Data, dataPoints[2].Z_Data);
printf("%lf %lf %lf %lf\n", dataPoints[3].Time_Data,
dataPoints[3].X_Data,dataPoints[3].Y_Data, dataPoints[3].Z_Data);
printf("%lf %lf %lf %lf\n", dataPoints[4].Time_Data,
dataPoints[4].X_Data,dataPoints[4].Y_Data, dataPoints[4].Z_Data);
printf("%lf %lf %lf %lf\n", dataPoints[5].Time_Data,
dataPoints[5].X_Data,dataPoints[5].Y_Data, dataPoints[5].Z_Data);
return 0;
}
The problem is that for some reason the Z_Data is being split into 2 and moved to the next entry in the array. The output I get is
0.000000 0.000000 0.000000 0.000000
2000.123000 2.849000 0.340000 -0.500000
43.000000 0.000000 0.000000 0.000000
2001.456000 3.493000 0.190000 -0.900000
87.000000 0.000000 0.000000 0.000000
2001.735000 5.403000 1.587000 0.500000
Which I'm not really sure why it is happening.
The length of each lines of your CSV file are:
time,identifier,x,y,z : 21 chars
2000.123,A123,2.849,0.34,-0.543 : 31 chars
2001.456,A123,3.493,0.19,-0.987 : 31 chars
2001.735,A123,5.403,1.587,0.579,-0.254 : 38 chars
You specified 30
as the buffer size for fgets()
. This value is insufficient for reading the data lines.
The value should be sizeof(line)
because you allocated 50 chars for line
and using magic numbers should be avoided.