I have a value I read in from a file and is stored as a char*. The value is a monetary number, #.##, ##.##, or ###.##. I want to convert the char* to a number I can use in calculations, I've tried atof and strtod and they just give me garbage numbers. What is the correct way to do this, and why is the way I am doing it wrong?
This is essentially what I am doing, just the char* value is read in from a file. When I print out the temp and ftemp variables they are just garbage, gigantic negative numbers.
Another Edit:
I am running exactly this in gcc
#include <stdio.h>
int main()
{
char *test = "12.11";
double temp = strtod(test,NULL);
float ftemp = atof(test);
printf("price: %f, %f",temp,ftemp);
return 0;
}
and my output is price: 3344336.000000, 3344336.000000
Edit: Here is my code
if(file != NULL)
{
char curLine [128];
while(fgets(curLine, sizeof curLine, file) != NULL)
{
tempVal = strtok(curLine,"|");
pairs[i].name= strdup(tempVal);
tempVal = strtok(NULL,"|");
pairs[i].value= strdup(tempVal);
++i;
}
fclose(file);
}
double temp = strtod(pairs[0].value,NULL);
float ftemp = atof(pairs[0].value);
printf("price: %d, %f",temp,ftemp);
my input file is very simple name, value pairs like this:
NAME|VALUE
NAME|VALUE
NAME|VALUE
with the value being dollar amounts
SOLVED: Thank you all, I was using %d instead of %f and didn't have the right headers included.
You are missing an include :
#include <stdlib.h>
, so GCC creates an implicit declaration ofatof
andatod
, leading to garbage values.And the format specifier for double is
%f
, not%d
(that is for integers).Code posted by you is correct and should have worked. But check exactly what you have in the
char*
. If the correct value is to big to be represented, functions will return a positive or negativeHUGE_VAL
. Check what you have in thechar*
against maximum values thatfloat
anddouble
can represent on your computer.Check this page for
strtod
reference and this page foratof
reference.I have tried the example you provided in both Windows and Linux and it worked fine.
This is your problem. Since the arguments are type
double
andfloat
, you should be using%f
for both (sinceprintf
is a variadic function,ftemp
will be promoted todouble
).%d
expects the corresponding argument to be typeint
, notdouble
.Variadic functions like
printf
don't really know the types of the arguments in the variable argument list; you have to tell it with the conversion specifier. Since you toldprintf
that the first argument is supposed to be anint
, printf will take the nextsizeof (int)
bytes from the argument list and interpret it as an integer value; hence the first garbage number.Now, it's almost guaranteed that
sizeof (int)
<sizeof (double)
, so whenprintf
takes the nextsizeof (double)
bytes from the argument list, it's probably starting with the middle byte oftemp
, rather than the first byte offtemp
; hence the second garbage number.Use
%f
for both.