Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double to Const Char*

Tags:

c++

char

double

How can I convert a double into a const char, and then convert it back into a double?

I'm wanting to convert the double to a string, to write it to a file via fputs, and then when I read the file, it will need to be converted back into a double.

I'm using Visual C++ 2010 Express Edition.

like image 226
Justin White Avatar asked Jun 19 '11 19:06

Justin White


2 Answers

If you just want to write the double values to file, you can simply write it, without converting them into const char*. Converting them into const char* is overkill.

Just use std::ofstream as:

 std::ofstream file("output.txt")'

 double d = 1.989089;

 file << d ; // d goes to the file!

 file.close(); //done!
like image 109
Nawaz Avatar answered Oct 14 '22 10:10

Nawaz


Since you added C++ to your tags, I suggest you use std::stringstream:

#include <sstream>

stringstream ss;
ss << myDouble;
const char* str = ss.str().c_str();
ss >> myOtherDouble;
like image 40
Paul Manta Avatar answered Oct 14 '22 08:10

Paul Manta