Writing to file with ofstream

In this snippet, we look at writing to a file using the fstream library. The fstream library provides an ofstream functionality for out file stream that we can use to write to a file

In the examples, we write to file output.txt some sample text

#include <string>
#include <fstream>
#include <iostream>

int main(){

    // Create a file with append operation
    std::ofstream my_file;

    my_file << "This is the first line \n";
    my_file << "This is the second line \n";

Compiling and running the code

$ g++ main.cpp -std=c++17 -o write_to_file
$ ./write_to_file

Cheking the content of the file

$ cat output.txt
This is the first line 
This is the second line