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 ?– ZeyadJun 11, 2013 at 12:37
-
-
3ofstream doesn't need std::ofstream::out flag because its present there by default .– HeavenHMJul 2, 2018 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. Nov 7, 2018 at 9:13
-
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.
-
-
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?)– KalenFeb 7, 2022 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.
Deleting the file will also remove the content. See remove file.
-
This answer has less code (single line!) and better readable code than the answer proposed by PureW. Feb 2, 2015 at 13:01
-
16My interpretation of the question was to clear contents of file, not delete the file.– PureWJan 13, 2016 at 0:27
-
@PureW: Deleting the file is one method of clearing the contents. Feb 14, 2016 at 3: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. FrankJun 28, 2019 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". Jun 28, 2019 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<<"";
}