Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

how to type cast string into float

hi, i've got a text file containing several lines of float numbers.
when i read those numbers in, the program always regards them as strings. how can i use them as float numbers?
thanks in advance!
[220 byte] By [WXY595] at [2007-11-11 10:24:25]
# 1 Re: how to type cast string into float
you can just read them in as floats, via >> operations

infile >> floatvar;

or you can convert them with atof (ascii to float)
jonnin at 2007-11-11 20:58:55 >
# 2 Re: how to type cast string into float
could u please give me more details of the implementation. thanks
WXY595 at 2007-11-11 21:00:02 >
# 3 Re: how to type cast string into float
open a file. use the >> operator, its exactly like "cin" or, call the function "atof" on a string. Im not sure what else to say about either one? Both are c++ language features.

ifstream inf;
inf.open("file.txt");
float f;
inf >> f;

or
char ch[1000];
inf>> ch;
f = atof(ch);
...
of course you can use string class or whatever as well...
jonnin at 2007-11-11 21:01:00 >