c++ write to file overwrite code example
Example 1: write in file cpp
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream my_file;
my_file.open("my_file.txt", ios::out);
if (!my_file) {
cout << "File not created!";
}
else {
cout << "File created successfully!";
my_file << "Guru99";
my_file.close();
}
return 0;
}
Example 2: write to file in C++
ofstream myfile;
myfile.open("file.txt");
myfile << "write this to file"