I am programming on C++. In my code I create a text file, write data to the file and reading from the file using stream, after I finish the sequence I desire I wish to clear all the data inside the txt file. Can someone tell me the command to clear the data in the txt file. Thank you
5 Answers
If you simply open the file for writing with the truncate-option, you'll delete the content.
std::ofstream ofs;
ofs.open("test.txt", std::ofstream::out | std::ofstream::trunc);
ofs.close();
-
1thanks .. does this option delete the content when I run this line or when I close the file ? – Zeyad Jun 11 '13 at 12:37
-
-
3ofstream doesn't need **std::ofstream::out** flag because its present there by default . – Haseeb Mir Jul 02 '18 at 16:19
-
7@HaSeeBMiR, that's incorrect. One does need specify both out and trunc flag here, otherwise it does not work. Yes out flag is default, but the moment one passed in trunc flag it is overridden. Give it a try. – Murphy Meng Nov 07 '18 at 09:13
-
-
@MurphyMeng I know this comment is old, but to anyone looking at this post, just know that both std::ios::out and std::ios::trunc are default operations for open. – Captain Hatteras Apr 22 '22 at 19:57
-
After many years of reading code, explicit beats default most days of the week :) – PureW May 04 '22 at 15:10
As far as I am aware, simply opening the file in write mode without append mode will erase the contents of the file.
ofstream file("filename.txt"); // Without append
ofstream file("filename.txt", ios::app); // with append
The first one will place the position bit at the beginning erasing all contents while the second version will place the position bit at the end-of-file bit and write from there.

- 131
- 1
- 4
-
-
there must be some file-system difference between these two solutions. perhaps truncate is insuring deletion of the bits entirely from the file system (or maybe the implementation SHOULD do so?) – Kalen Feb 07 '22 at 20:10
If you set the trunc flag.
#include<fstream>
using namespace std;
fstream ofs;
int main(){
ofs.open("test.txt", ios::out | ios::trunc);
ofs<<"Your content here";
ofs.close(); //Using microsoft incremental linker version 14
}
I tested this thouroughly for my own needs in a common programming situation I had. Definitely be sure to preform the ".close();" operation. If you don't do this there is no telling whether or not you you trunc or just app to the begging of the file. Depending on the file type you might just append over the file which depending on your needs may not fullfill its purpose. Be sure to call ".close();" explicity on the fstream you are trying to replace.

- 133
- 1
- 10
Deleting the file will also remove the content. See remove file.

- 56,849
- 17
- 98
- 154
-
This answer has less code (single line!) and better readable code than the answer proposed by PureW. – Tim Kuipers Feb 02 '15 at 13:01
-
16My interpretation of the question was to clear contents of file, not delete the file. – PureW Jan 13 '16 at 00:27
-
@PureW: Deleting the file is one method of clearing the contents. – Thomas Matthews Feb 14 '16 at 03:11
-
Does it clear the content from disk, or just remove the file descriptor pointing to the file? I think it only does the later, it does not actually shred or wipe the file from disk. – K. Frank Jun 28 '19 at 21:18
-
What it does is OS dependent. Most File Systems will mark the file in the File Allocation Table (FAT) as deleted. The actual sectors containing the data will not be altered. If you need a secure delete, you'll have to search the internet for "c++ secure file erase". – Thomas Matthews Jun 28 '19 at 21:43
You should create a function which clears all the data of the file and then run it.
void clear()
{
ofstream file("fileout.txt");
file<<"";
}

- 2,402
- 2
- 29
- 35