A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
For writing and later reading float values between C++ programs, use standard file I/O streams. You can choose text or binary format depending on your needs.
Text-mode I/O (human-readable)
#include <fstream>
void write_floats_text(const char* filename, float a, float b, float c)
{
std::ofstream out(filename); // text mode by default
out << a << ' ' << b << ' ' << c; // formatted text output
}
void read_floats_text(const char* filename, float& a, float& b, float& c)
{
std::ifstream in(filename);
in >> a >> b >> c; // formatted text input
}
This uses ofstream/ifstream and the stream insertion (<<) and extraction (>>) operators. It is portable and easy to inspect, but may lose some precision depending on formatting.
Binary-mode I/O (exact bit pattern)
#include <fstream>
void write_floats_binary(const char* filename, float a, float b, float c)
{
std::ofstream out(filename, std::ios::binary);
out.write(reinterpret_cast<const char*>(&a), sizeof(a));
out.write(reinterpret_cast<const char*>(&b), sizeof(b));
out.write(reinterpret_cast<const char*>(&c), sizeof(c));
}
void read_floats_binary(const char* filename, float& a, float& b, float& c)
{
std::ifstream in(filename, std::ios::binary);
in.read(reinterpret_cast<char*>(&a), sizeof(a));
in.read(reinterpret_cast<char*>(&b), sizeof(b));
in.read(reinterpret_cast<char*>(&c), sizeof(c));
}
This uses ofstream/ifstream in binary mode and the write/read member functions. It preserves the exact float representation, but the file is not human-readable and both programs must agree on layout and endianness.
For simple cross-program exchange on the same platform, either approach is valid. Use text mode for readability and easier debugging; use binary mode when exact bit-level fidelity and performance are more important.
References: