# Casting in C



## C0B01 (Jan 6, 2005)

Another simple question.. How do you cast a string variable (array of chars) into a float?


C


----------



## LoneWolf071 (Nov 10, 2004)

Well, To Do Char to int or float, use the atoi() or atof() function, do you want each char of he string to have it's number stored, or all the values added together? Look Through the stdlib.h file, or, in linux, do a man strtol()...


----------



## C0B01 (Jan 6, 2005)

The program reads lines from a .dat file. Each line in the file consists of 6 floating point numbers, I've got it to read the data through fscanf and store the read data into 6 char arrays, to be able to do the calculations later in program I need to convert these char arrays into floats so I can actually do some math on them.


C


----------



## johnwill (Sep 26, 2002)

Why not read them directly into float data types? fscanf() will do that for you... Why read them into character arrays?

If you must read then in ASCII, you can use sscanf() to convert the ASCII floating point characters into a float.

I'm assuming the file format looks something like this:

123.00 111.00 222.00 333.00 444.00 555.00

Or perhaps it's comma delimited?

I'd just do an fgets() of the whole file line, then use sscanf() to parse it into floats.


----------

