C++:
Writing a text file

How to:

C++ offers several ways to write to a text file, but one of the most straightforward methods is using the <fstream> library which provides the ofstream (output file stream) class designed for file writing operations.

Example using <fstream>:

#include <fstream>
#include <iostream>

int main() {
    std::ofstream file("example.txt");
    if (file.is_open()) {
        file << "Hello, world!\n";
        file << "Writing to a file in C++ is simple.";
        file.close();
    } else {
        std::cerr << "Failed to open file\n";
    }
    return 0;
}

Sample output in ’example.txt’:

Hello, world!
Writing to a file in C++ is simple.

When dealing with more complex data or needing more control over the writing process, programmers might turn to third-party libraries such as Boost Filesystem.

Example using Boost Filesystem:

To use Boost for file operations, you’ll first need to install the Boost libraries. The following example demonstrates creating and writing to a file using boost::filesystem and boost::iostreams.

#include <boost/filesystem.hpp>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/stream.hpp>
#include <iostream>

namespace io = boost::iostreams;
namespace fs = boost::filesystem;

int main() {
    fs::path filePath("boost_example.txt");
    io::stream_buffer<io::file_sink> buf(filePath.string());
    std::ostream out(&buf);
    out << "Boost makes file operations easy.\n";
    out << "This is a line written with Boost.";
    
    return 0;
}

Sample output in ‘boost_example.txt’:

Boost makes file operations easy.
This is a line written with Boost.

The choice between raw C++ and a third-party library like Boost may depend on the specific requirements of your project and how much control or flexibility you need over file I/O operations.