I have to read a line like this:
0.000000 0.000000 -1.000000 -1.000000 0.230392 0.562016 -1.000000 -1.000000
Using strtok()
and a while
loop, I'm trying to extract each float numer and store it on my program. There is a problem when converting the token to a floating point number. This is my code:
double p;
char* token = strtok(line, " ");
while (token) {
p = atof(token);
printf("atof('%s') = %f\n", token, p);
token = strtok(NULL, " ");
}
It outputs:
atof('0.000000') = 0,000000
atof('0.000000') = 0,000000
atof('-1.000000') = -1,000000
atof('-1.000000') = -1,000000
atof('0.230392') = 0,000000 <<<---- ???????????
atof('0.562016') = 0,000000 <<<---- ???????????
atof('-1.000000') = -1,000000
atof('-1.000000') = -1,000000
See the problem? Why does atof return 0 when passing a string number between 0 and 1?