Hi,

How can i delete the file in the middle of the program in C++

Thanks

Dani AI

Generated

The OP () asked a simple question and the replies covered the usual options: close any active streams and call a library delete, use Boost.Filesystem, or fall back to platform APIs (as , and noted). Below are practical, cross‑platform clarifications and a safe pattern to follow so the deletion behaves predictably at run time.

Streams and open handles matter. std::fstream closes its file on destruction/close (RAII), so make sure you close or let the stream go out of scope before attempting to remove/replace the file. POSIX unlink removes the directory entry but the file data remains until the last open file descriptor is closed; Windows DeleteFile will fail (or mark the file for deletion) if other handles are open unless those handles were opened with delete-sharing. See std::basic_fstream, unlink(2) and DeleteFile documentation for the details. (en.cppreference.com)

If you want to replace a file atomically (typical for “save” operations), use the write‑to‑temporary + swap pattern: write to a temp file, flush/ensure the data is on disk (fsync on POSIX if you need durability), close the temp file, then rename/swap the temp into place. On POSIX a rename that overwrites an existing name is atomic; on Windows prefer ReplaceFile for a comparable replace-with-backup operation if you need Windows semantics. For portable C++ code prefer std::filesystem for the rename/remove operations. Example pattern:

namespace fs = std::filesystem;
fs::path tmp = fs::temp_directory_path() / "myfile.tmp";
std::ofstream ofs(tmp);
ofs << contents;
ofs.close();                 // important before rename on Windows
fs::rename(tmp, target_path); // swap into place (POSIX atomic when same FS)

See std::filesystem and the POSIX rename/Windows ReplaceFile docs for platform caveats. (en.cppreference.com)

Finally, handle errors (check return values or catch std::filesystem::filesystem_error), verify permissions, and beware network filesystems where atomic guarantees can differ. If the program runs on both POSIX and Windows, prefer std::filesystem for clarity and add small platform-specific branches only for durability/atomicity requirements. (en.cppreference.com)

Recommended Answers

All 13 Replies

Hi,
How can i delete the file in the middle of the program in C++

Probably in the same manner as in the program head or in the program tail: close all streams associated with the file then use remove(filepath) library function declared in <cstdio>.

boost is unnecessary - anytime anyone suggest a usage.

boost is unnecessary - anytime anyone suggest a usage.

Are you serious? Far from true.

Fortunately, the remove from <cstdio> is a standard library function so no need to fire a nuclear missile to kill a fly. It's an absolutely portable function ;)

It's a great library that should be put to use. It may not be necessary in this case but there's nothing wrong with suggesting alternatives.

It would be perfectly reasonable to use in this case if the programmer has already put Boost in their toolkit.

Are you serious? Far from true.

I should clarify here. Yes, Boost is unnecessary but so is cstdio in this context. But then so is the entire language of C++ or any other high-level language, for that matter.

Every library is there to make our life a little easier and so we don't have to re-invent the wheel everyday. Boost is a great example and should be put to use if someone finds it helpful.

commented: Making stupid statements does not help you prove a point. -3

regexes can be worthwhile.

I should clarify here. Yes, Boost is unnecessary but so is cstdio in this context. But then so is the entire language of C++ or any other high-level language, for that matter.

Every library is there to make our life a little easier and so we don't have to re-invent the wheel everyday. Boost is a great example and should be put to use if someone finds it helpful.

Let's come back to the OP. Is it a question like how to delete file with Boost library? Or it was How can i delete the file in the middle of the program in C++? Correct me, please, if I'm wrong and it was Boost-oriented queston.
Otherwise let's discuss all great (and not only great) libraries where remove file functions exist. Let's consider how to use the Great Boost Library instead STL for streams, big num libraries instead of builtin operator +(POD,POD) and so on...
Keep it simpler ;)...

commented: Adding to the stupidity is not necessary. As you say, back to the OP. -3

Let's come back to the OP. Is it a question like how to delete file with Boost library? Or it was How can i delete the file in the middle of the program in C++? Correct me, please, if I'm wrong and it was Boost-oriented queston.
Otherwise let's discuss all great (and not only great) libraries where remove file functions exist. Let's consider how to use the Great Boost Library instead STL for streams, big num libraries instead of builtin operator +(POD,POD) and so on...
Keep it simpler ;)...

You questioned the merits of Boost. I simply disagreed and stated my opinion, just as you did. ;)

But yes, I did get a little off topic in the last comment, but I thought the clarification was due.

I certainly think my comments have been much more on topic than "boost is unnecessary - anytime anyone suggest a usage."

I'm not sure I understand your emphasis of "in C++". It's as if you're questioning Boost as C++. I would suggest it's more C++ than cstdio, a relic from C.

What's not simple about Boost? And why does an answer have to be simple anyway?

You questioned the merits of Boost. I simply disagreed and stated my opinion, just as you did. ;)

But yes, I did get a little off topic in the last comment, but I thought the clarification was due.

I certainly think my comments have been much more on topic than "boost is unnecessary - anytime anyone suggest a usage."

I'm not sure I understand your emphasis of "in C++". It's as if you're questioning Boost as C++. I would suggest it's more C++ than cstdio, a relic from C.

What's not simple about Boost? And why does an answer have to be simple anyway?

It seems the patient's case is hopeless ;)...
Apropos, <cstdio> is not a relict. It's a part of the C++ Standard (the current and the next).

It seems the patient's case is hopeless ;)...

No need for comments like these. ;) I've done nothing but try to help others in my short time here and I've seen many of your comments and assume you try to do the same.

Apropos, <cstdio> is not a relict. It's a part of the C++ Standard (the current and the next).

If I've suggested otherwise, you've misunderstood me. I'm not sure relict is the best term but I wouldn't take the statement back because I think it pretty accurately describes my thought in that context. That's not to say it doesn't have use or that it shouldn't be used.

I wasn't trying to make a big issue out of this. I was simply trying to suggest that boost::filesystem is an option and a very reasonable one at that. Any further comments were an effort to get an explanation from those that think otherwise. This has yet to come.

If there's no interest in discussing the merit of Boost, I'll consider this discussion over.

Back on topic, before this is locked. The OP can also use POSIX unlink(), Win32 DeleteFile(), or even in desperation system(). Obviously remove() takes care of all of that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.