If you work with only one system, you can write the actual binary data to a file, which will give you an exact copy. If you want to write to a text file, convert the binary to Base64 or something like that.
std::ofstream myfile("file.bin"); double x; myfile.write(reinterpret_cast<char*>(&x), sizeof(x));
Optionally, encode the byte stream as base64. If you want to store long doubles and you are going to encode base64, note that the long double is usually only 10 bytes, so you can ignore the addition that it usually gets when serializing.
If you plan on exchanging data between different platforms, but all of which use IEEE floats, you should probably be more careful and document file format compliance. In this case, writing an ASCII string encoded with base64 will be more portable. To exchange data between platforms that do not all use the same binary floating-point representation, you will have to work even harder ...
source share