51

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

1

5 Answers 5

82

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();

http://www.cplusplus.com/reference/fstream/ofstream/open/

7
  • 1
    thanks .. does this option delete the content when I run this line or when I close the file ?
    – Zeyad
    Jun 11, 2013 at 12:37
  • The content is deleted when you open with std::ofstream::trunk.
    – PureW
    Jun 11, 2013 at 17:22
  • 3
    ofstream doesn't need std::ofstream::out flag because its present there by default .
    – HeavenHM
    Jul 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
  • Thats why you have to use bitwise OR '|' to get both working.
    – HeavenHM
    Nov 7, 2018 at 12:51
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.

2
  • This is the simplest answer I have found! Jun 15, 2020 at 14:59
  • 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 7, 2022 at 20:10
7

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.

5

Deleting the file will also remove the content. See remove file.

5
  • This answer has less code (single line!) and better readable code than the answer proposed by PureW. Feb 2, 2015 at 13:01
  • 16
    My interpretation of the question was to clear contents of file, not delete the file.
    – PureW
    Jan 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. Frank
    Jun 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
0

You should create a function which clears all the data of the file and then run it.

void clear()
{
    ofstream file("fileout.txt");
    file<<"";
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.