By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

C++ Program to delete itself after execution ?

P: n/a
Greetings,
I hear this question is one famous interview question . Just
wondering what could be the ways to do this ? Does storing process
info in a text file while running and invoking a small background
script on atexit() a good solution ? if the script keeps pinging for
the pid & binary name and delete it if not found?

Cheers
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Mike" <mi*********@yahoo.com> wrote...
Greetings,
I hear this question is one famous interview question . Just
wondering what could be the ways to do this ? Does storing process
info in a text file while running and invoking a small background
script on atexit() a good solution ? if the script keeps pinging for
the pid & binary name and delete it if not found?


This has nothing to do with the subject of comp.lang.c++. Please
post your question to a newsgroup that deals with your OS.
Jul 22 '05 #2

P: n/a
mi*********@yahoo.com (Mike) wrote in
news:bb*************************@posting.google.co m:
I hear this question is one famous interview question . Just
wondering what could be the ways to do this ? Does storing process
info in a text file while running and invoking a small background
script on atexit() a good solution ? if the script keeps pinging for
the pid & binary name and delete it if not found?


I've heard it's the famous "how do I write a virus" question. ;)

This question is actually quite platform specific, by the way. Please ask
in a forum appropriate for your platform.
For MS Windows, see news:comp.os.ms-windows.programmer.*
For unix-related, see news:comp.unix.programmer

Cheers.
Jul 22 '05 #3

P: n/a
Mike wrote:
Greetings,
I hear this question is one famous interview question . Just
wondering what could be the ways to do this ? Does storing process
info in a text file while running and invoking a small background
script on atexit() a good solution ? if the script keeps pinging for
the pid & binary name and delete it if not found?

Cheers


#include <cstdlib>
#include <cstdio>

int main(int argc, char * * argv)
{
remove(argv[0]);
return EXIT_SUCCESS;
}

Provided that the program was launched from a file.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #4

P: n/a
In message <0J***************@newssvr15.news.prodigy.com>, Thomas
Matthews <Th****************************@sbcglobal.net> writes
Mike wrote:
Greetings, I hear this question is one famous interview question
. Just
wondering what could be the ways to do this ? Does storing process
info in a text file while running and invoking a small background
script on atexit() a good solution ? if the script keeps pinging for
the pid & binary name and delete it if not found?
Cheers


#include <cstdlib>
#include <cstdio>

int main(int argc, char * * argv)
{
remove(argv[0]);
return EXIT_SUCCESS;
}

Provided that the program was launched from a file.

.... under a cooperative OS.

--
Richard Herring
Jul 22 '05 #5

P: n/a
[snips]
#include <cstdlib>
#include <cstdio>

int main(int argc, char * * argv)
{
remove(argv[0]);
return EXIT_SUCCESS;
}

Provided that the program was launched from a file.


And on a platform/implementation that fills argv[0] with useful
information. And on a platform/implementation that allows you to delete
executables. And on a platform/implementation that doesn't lock
executables while they're running. And on a platform/implementation that
doesn't suport symbolic links, or passes the link's target, rather than
the link itself, in usable format, to argv[0]. And on a
platform/implementation for which you have delete/write/whatever
permissions to the file. And...


Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.