I have the following situation wherein I'm reading a file line-by-line where the first few lines (example) are as follows:
0.00000000000000000, 0.00000000000000000, 0.00000000000000000, 20.00000000000000000, 0.18854095325733133
0.00000000000000000, 0.00000000000000000, 0.00000000000000000, 28.88888888888888900, 0.18861310382752311
0.00000000000000000, 0.00000000000000000, 0.00000000000000000, 37.77777777777777900, 0.18870756072971304
0.00000000000000000, 0.00000000000000000, 0.00000000000000000, 46.66666666666667100, 0.18876790723761561
0.00000000000000000, 0.00000000000000000, 0.00000000000000000, 55.55555555555555700, 0.18879081871602224
and the processing of the lines:
fp = fopen(fn, "r");
if (fp == NULL) {
<...>
}
while ((read = getline(&line, &len, fp)) !=-1) {
<...>
char *pt;
pt = strtok(line,",");
while (pt != NULL) {
//order: pressure, velocity, strain, temp
pnn = atof(pt); //pressure
pt = strtok(NULL, ",");
vrm = atof(pt); //velocity
pt = strtok(NULL, ",");
epx = atof(pt); //elastic strain
pt = strtok(NULL, ",");
temp = atof(pt); //temperature
pt = strtok(NULL, ",");
//reference friction coefficient
REAL ref_ffric = (REAL)atof(pt);
ref_fric = &ref_ffric;
pt = strtok(NULL, ",");
REAL
is in this context defined as a float
However the ref_fric
is never the correct amount, i.e the first line it should be 0.18854095325733133 but rather when I print it out it's 0.18854095041751862.
How do I solve this char* to float conversion precision problem?