A
A
Anton2017-09-27 08:44:07
C++ / C#
Anton, 2017-09-27 08:44:07

How to clear a file using file I/O streams without closing it?

I want to open a file, write a line to it, then, without closing the file, start writing again, so that the old information is deleted:

std::ofstream file;
file.open("file.txt", std::ios_base::in | std::ios_base::out);
file << "AA";
file.seekp(std::ios::beg);
file << "1";
file.close();

As a result, I get 1Aand should be1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fil, 2017-09-27
@Riki-tiki-tavi

Exactly the way you want - no way.
How to clear the content of a file after it is ope...
How to truncate a file while it is open with fstream

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question