0

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;
}
Community
  • 1
  • 1
Jesús Ros
  • 480
  • 2
  • 6
  • 19
  • 1
    what do you get when you print the lat/lon tokens before converting them? – AShelly Dec 30 '13 at 17:05
  • I get them right as strings. So the problem is in the `strtod()` function. Any clue on why it is not working properly in windows? – Jesús Ros Dec 30 '13 at 17:26

1 Answers1

1

Perhaps you need to #include <stdlib.h>

AShelly
  • 34,686
  • 15
  • 91
  • 152
  • Damn... By the way, why in the code I mention for mystrsep() the #include library is linked? Why is it necessary since I don't remember linking it ever on Unix? – Jesús Ros Dec 30 '13 at 17:48
  • 'stddef.h' is an include file, not a library. There is nothing to link - it just provides some definitions. But as far as I can tell, it is not strictly needed in the code from the other question. – AShelly Dec 30 '13 at 17:52