ShaolinGOD ShaolinGOD - 1 month ago 10
C Question

How would I get strtok() to return the decimal number and not whole numbers?

I have a function that reads in a file and prints it out as a grid.

This is the file (input):

2 2
1.83 5.64
7.36 4.10


But when it prints out the matrix it only returns whole numbers (output):

2 2
1.00 5.00
7.00 4.00


How do I get the function to print out the decimal number?

Wanted Output (i.e):

2 2
1.83 5.64
7.36 4.10

Answer

Your problem isn't strtok, it's this:

double newVal = strtol(value, &ptr, 10);

strtol converts the string to a long, not a double, so it's discarding the fractional portion. Use strtod instead:

double newVal = strtod( value, &ptr );