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 fput
You can use these functions to convert to and from:
template
bool convertFromStr(string &str, T *var) {
istringstream ss(str);
return (ss >> *var);
}
template
string convertToStr(T *var) {
ostringstream ss;
ss << *var;
return ss.str();
}
Example:
double d = 1.234567;
string str = convertToStr(&d);
cout << str << endl;
double d2;
convertFromStr(str, &d2);
cout << d2 << endl;