Write to file (ofstream)

If the file is only written to, the class ofstream can be used.

Open a file

ofstream myFile;
myFile.open(filename, ios::out);

Example

#include <fstream>
#include <iostream>
using namespace std;

int main() {
    string str;
    ofstream myFile;
    myFile.open("textfile.txt", ios::out);
    if (myFile.is_open()) {
        myFile << "New line to text file" << endl;
        myFile << "Second line to text file" << endl;
        myFile.close();
    } else {
        cout << "Unable to open text file";
    }

    return 0;
}
Output
[textfile.txt]
New line to text file
Second line to text file