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.
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!
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With