C++ Variable I/O

Sid Kraft 46 Reputation points
2026-07-12T13:14:08.5033333+00:00

Developing a C++ program and have a requirement of outputting several float variables to a file, to later read them back into another program. What I/O statements would be used to do this? Sid Kraft

Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other

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.

0 comments No comments

1 answer

Sort by: Most helpful
  1. AI answer

    2026-07-12T13:14:25.05+00:00

    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:

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.