If the file is only written to, the class ofstream
can be used.
ofstream myFile;
myFile.open(filename, ios::out);
#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;
}
[textfile.txt]
New line to text file
Second line to text file