I have a file that contains multiple lines with the following format:
2019-12-05 07:14:00 (151.45,-23.917)
I want to split each line into 3 variables so that the first variable is "2019-12-05", the second variable is "07:14:00" and the final variable is (151.45,-23.917) in the example of the above line shown.
I have a snippet of code below in which I tried to split the lines into three variables but I get a segmentation fault.
int main(int argc, char *argv[])
{
FILE *fp=NULL;
char inpFname[81],buf[8000];
int dates,hour,lon,lat;
int i=0;
int voyageid = atoi(argv[1]);
sprintf(inpFname,"%d_latlon.txt",voyageid);
if ((fp=fopen(inpFname,"rt"))==NULL)
{
printf("\nERROR: Cannot open/read input file [%s]\n\n",inpFname);
exit(1);
}
while(!feof(fp))
{
fgets(buf,8000,fp);
printf("%d) buf is %s",i,buf);
sscanf(buf,"%s %s %s", dates,hour,lon);
printf("date is %s and lat is %s and lon is %s\n",dates,lat,lon);
i=i+1;
}
}
How can I tweak the code to ensure a successful split of each line into separate variables?
The file being input looks like this:
2019-12-03 06:00:00 (152.282,-13.505)
2019-12-03 12:00:00 (152.393,-14.8478)
2019-12-03 18:00:00 (152.505,-16.1906)
2019-12-04 00:00:00 (152.618,-17.5335)
2019-12-04 06:00:00 (152.732,-18.8763)
2019-12-04 12:00:00 (152.846,-20.2191)
2019-12-04 18:00:00 (152.962,-21.5619)