A truck trajectory is represented by a sequence of location points of the truck
ID: 3746906 • Letter: A
Question
A truck trajectory is represented by a sequence of location points of the truck reported at different time. Her we show an example trajectory that consists of location points reported at a 30-second interval. Every locatio point (a line) has five columns, which represent the date (day/month/year), time (hour:minute:second), latitud longitude, and unique id (6 characters) of the location point, respectively 10/9/02 9:15:59 23.845089 38.018470 DXUYHu 10/9/02 9:16:29 23.845179 38.018069 tKoPTx 10/9/02 9:16:59 23.845530 38.018241 JPQbNb 10/9/02 9:17:29 23.845499 38.017440 aEWdXS 10/9/02 9:17:59 23.844780 38.015609 gqeEjx 10/9/02 9:18:29 23.844780 38.014018 aQArkX 10/9/02 9:18:59 23.844869 38.012569 fhQIAS 10/9/02 9:19:29 23.845360 38.011600 BhngfQ 10/9/02 9:19:59 23.845550 38.010650 rgwehm 10/9/02 9:20:29 23.845100 38.010478 jdBgpN For example, the first line of the sequence represents a location point reported at 9:15:59 on 10/9/02 with coordinate of (23.845089, 38.018470). This data point has a unique id of "DXUYHu"Explanation / Answer
In all such problems where you have to parse some text data and then tokenize the line to extract few fields, use strtok() or strtok_r() functions. Following is a sample code to read, tokenize and fill the data into two separate arrays -
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fp = fopen("data.dat", "r+");
char buf[256];
char *latitude[1024], *longitude[1024];
int i;
char *s;
if (fp == NULL) {
printf("file open failed ");
return -1;
}
while(!feof(fp)) {
// read the file line by line
memset(buf, 0, 256);
fgets(buf, 256, fp);
if (buf[0] == '') {
break;
}
printf("line: %s ", buf);
s = strtok(buf, " "); //returns the first token i.e the date
printf("str1: %s ", s);
s = strtok(NULL, " "); //returns the second token i.e the time
printf("str2: %s ", s);
s = strtok(NULL," "); // returns the third token i.e the latitude
// copy the string into newly allocated memory as the original memory belongs to buf
printf("str3: %s ", s);
latitude[i] = strdup(s);
s = strtok(NULL," "); // returns the fourth token i.e the longitude
printf("str4: %s ", s);
// copy the string into newly allocated memory as the original memory belongs to buf
longitude[i++] = strdup(s);
//continue and read the next line
}
int j;
for (j = i-1; j >=0; j--) {
printf("%s : %s ", latitude[j], longitude[j]);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.