Having an issue with the following code. I read in a decimal value from a text file, use atof (or strtod, either gives the same error) to convert the read value into a floating point number. When putting the output of atof in a double, it works fine. However when putting the output of atof into a float, the decimal places get lost. Putting the result in a double, then into a float gives the same result. This only happens when using getline.
I've tried using temp arrays to put the result of pch in, sprintfs and such to try and work around it, but whenever a value originates from the getline command, the float value always loses decimal places. I've also tried varying the precision in the printf statement, but its always the same. When I write the float result to a file via FILE.write in binary mode, the result comes out without the decimal places when I read it back in later.
The simple solution is to not use float and use double, but there are a number of reasons I'm using float to begin with and don't want to change that.
Side note, this code works fine on a windows machine, the error is coming from using g++ on a mac. ANy insight would be appreciated.
code outputCode:printf("Reading from File\n");
ifstream FILE(argv[2],ios::in);
char BU[128];
char *pch;
// skipping lines to get to the numebers
FILE.getline(BU,512);
FILE.getline(BU,512);
FILE.getline(BU,512);
FILE.getline(BU,512);
pch=strtok(BU," ");
printf("%s\n",pch);
double dval=atof(pch);
float fval=dval;
printf("%f\n",dval);
printf("%f\n",fval);
FILE.close();
Reading from File
338769.0109
338769.010900
338769.000000

