Self-made strsep() in windows

934 views Asked by At

I am trying to port some code I have in linux to Windows but I am having problems at porting the strsep() function. I have looked up in the forums and found this: https://stackoverflow.com/a/8514474/2833912

but it is not parsing correctly the field that contain a double value. The lines in the file are like:

node|171353||||||||-15.425|45.325

and I am interested in extracting the second and the two last fields as unsigned long and double, respectively. Using the mystrsep() function from that link and the following code I obtain the second field parsed correctly but the 2 double fields get printed as "7014352.000000" or "-1.#IND00", which are not correct.

int main(void)
{
  FILE *fp;
  char lineBuf[80000];
  char *token;
  double lat,lon;
  unsigned long id,i;

  if((fp = fopen("spain.csv", "r"))==NULL){
      fprintf (stderr, "\nError when opening file\n");
       return ;
    }

    for(i=0; i<10; i++){
        fgets (lineBuf, sizeof(lineBuf), fp);
        char *line  = lineBuf;
        token=mystrsep(&line,"|\n"); //node
        token=mystrsep(&line,"|\n"); id=strtoul(token,NULL,0);//id
        token=mystrsep(&line,"|\n"); //name
        token=mystrsep(&line,"|\n"); //place
        token=mystrsep(&line,"|\n"); //highway
        token=mystrsep(&line,"|\n"); //route
        token=mystrsep(&line,"|\n"); //ref
        token=mystrsep(&line,"|\n"); //oneway
        token=mystrsep(&line,"|\n"); //maxspd
        token=mystrsep(&line,"|\n"); lat=strtod(token,NULL);//lat
        token=mystrsep(&line,"|\n"); lon=strtod(token,NULL);//lon
        printf("id=%lu lat=%f lon=%f\n",id,lat,lon);
    }

  return 0;
}
1

There are 1 answers

2
AShelly On BEST ANSWER

Perhaps you need to #include <stdlib.h>