Reputation: 4687
I wanted to know if I can delete the values (string or int) written in a file from main. i.e. erase entire data in a text file and make it empty like before If yes, how?
Upvotes: 0
Views: 4876
Reputation: 361352
You can use ios_base::trunc
openmode with ofstream:
(truncate) Any current content is discarded, assuming a length of zero on opening.
Example,
std::ofstream ofile("filename.txt", ios_base::trunc);
//work with ofile
ofile.close();
Upvotes: 3
Reputation: 272477
Just overwrite it with a new, empty file:
#include <fstream>
std::ofstream ofs("myfile.txt");
ofs.close();
Upvotes: 4