# converting float to char* in C++

## Recommended Posts

matrisking    282
Is there an easy way to convert a float to char*? I'd like to be able to pass a float to the output stream initializer along with an extension. So, for example, a list of floats with the first number in the list indicates the ID of the list (cannot just name the lists the ID in this case), so I would like to extract the first entry of the list, add a .nmut extention to it, and create an ouput stream for a file called (for example) "2.nmut" Thanks!

##### Share on other sites
Enigma    1410
std::ofstream file((boost::lexical_cast< std::string >(my_float) + "my_extension").c_str());// orstd::ostringstream convertor;convertor << my_float << "my_extension";std::ofstream file(convertor.str().c_str());

Σnigma

##### Share on other sites
MaulingMonkey    1730
std::string pi = boost::lexical_cast< std::string >( 3.141592653589f );const char * as_c_string = pi.c_str();

EDIT: Geeze, didn't even see Enigma had posted that allready :-(. I fail.