accessing computer files.

Ok so early i saw a post about self destructing itself. Which is really just deleting itself. So i was wondering how in my code do i access computer files to do that? I'm a beginner.


Removing files is an OS-specific function.

Removing your own executable while running may not be possible (it isn't on Windows, at least).
you can do all sorts of things with system ("command") ;
where command is a CMD instruction ...
just execute the command remove() in C++:

for example to delete myFile.txt, this program does it
int main()

Edit: the file has to be located in the same directory of the program. Otherwise, you have to give the full path. So for Windows:


on linux:


Some compilers accept the linux path for Windows as well. I think the new C++ standard considers the Windows path form deprecated.

Last edited on
remove() is POSIX - it does not necessarily exist in Windows.
On Windows, you should use DeleteFile().

If you use Boost Filesystem, you can use boost::filesystem::remove().
Last edited on
I was thinking that, but I somehow couldn't find it so I thought I had made it up.

You are right, std::remove() is part of the C standard in <stdio.h>
Topic archived. No new replies allowed.