I need to open a file using ofstream for appending.
However I first want to make sure that the file does not exist, and if it does I want to delete all of the data inside.
How can I do this?
Printable View
I need to open a file using ofstream for appending.
However I first want to make sure that the file does not exist, and if it does I want to delete all of the data inside.
How can I do this?
Try this. If the file exists, it will "truncate" the contents of the file after opening it.
If you just want to delete a file, use remove(filename) and #include <stdio.h>Code:char *filename = "info.txt";
ofstream out(filename,ios::trunc);
if (!out)
{
cout << "Unable to open file " << filename << endl;
return 1;
}
perfect,
Thanks a ton!