28 December 2015

C++ - Write a number to a file.

For debugging a C++ DLL, I needed a way to write the value of a numeric variable to a file. This is how:

Includes needed

#include <sstream>
#include <fstream>

Convert the number

You can convert the number to a string using a stringstream.
std::stringstream convert;
convert << 305.24;

Output to a file

The result of the following is a text file at the DLL's location containing the number.
std::ofstream out("Output.txt");
out << "Value: ";
out << convert.str();
out.close();