Apr 2, 2012 at 3:57am UTC
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.
Thanks,
Hunter
Apr 2, 2012 at 4:47am UTC
Removing files is an OS-specific function.
Removing your own executable while running may not be possible (it isn't on Windows, at least).
Apr 2, 2012 at 6:50am UTC
you can do all sorts of things with system ("command" ) ;
where command is a CMD instruction ...
Apr 2, 2012 at 7:40am UTC
just execute the command remove() in C++:
for example to delete myFile.txt, this program does it
1 2 3 4
int main()
{
remove("myFile.txt" );
}
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:
"C:\\myFile.txt"
on linux:
"C:/myFile.txt".
Some compilers accept the linux path for Windows as well. I think the new C++ standard considers the Windows path form deprecated.
Cheers.
Last edited on Apr 2, 2012 at 7:54am UTC
Apr 2, 2012 at 10:38pm UTC
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 Apr 3, 2012 at 1:58am UTC
Apr 3, 2012 at 1:59am UTC
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>