How to write a program such that it will delete itself after
exectution?

Answers were Sorted based on User's Feedback



How to write a program such that it will delete itself after exectution?..

Answer / pranjali

I guess if your program name is program.c the last line of
your code should be the operating system call to to delete
the file "program.c".

once you compile it to get a .exe , the existance of
program.c is immeterial.

you will hv to take care of necessary error handling.

Is This Answer Correct ?    2 Yes 2 No

How to write a program such that it will delete itself after exectution?..

Answer / siva

this code is tested succesfully.

#include <windows.h>

#include <iostream>
#include <stdio.h>

#include <string.h>

using namespace std;

int main(int arg, int argv[])
{

STARTUPINFO si;
PROCESS_INFORMATION pi;
STARTUPINFO sj;
PROCESS_INFORMATION pj;

ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );

ZeroMemory( &sj, sizeof(sj) );
sj.cb = sizeof(sj);
ZeroMemory( &pj, sizeof(pj) );

if(!CreateProcess("C:\\Windows\\system32
\\cmd.exe", "/k del C:\\testp.exe", NULL, NULL, FALSE, 0,
NULL, NULL, &sj, &pj))
{
printf( "Hello CreateProcess failed (%d)\n",
GetLastError() );
}

return 0;
}

Is This Answer Correct ?    2 Yes 2 No

How to write a program such that it will delete itself after exectution?..

Answer / ven

I am not sure but I guess create a destructor

Is This Answer Correct ?    2 Yes 10 No

Post New Answer

More C++ General Interview Questions

What are the conditions that have to be met for a condition to be an invariant of the class?

1 Answers  


You're given an array containing both positive and negative integers and required to find the sub-array with the largest sum (O(N) a la KBL). Write a routine in C for the above.

4 Answers  


What is the difference between public, private, protected inheritance?

12 Answers   Wipro,


State the difference between pre and post increment/decrement operations.

0 Answers  


What are references in c++? What is a local reference?

0 Answers  






What is &x in c++?

0 Answers  


What is this pointer in c++?

1 Answers  


Does c++ have arraylist?

0 Answers  


What is the use of static functions?

10 Answers   Symphony,


Why are pointers not used in c++?

0 Answers  


Write a Program for read a line from file from location N1 to N2 using command line arguments. Eg:exe 10 20 a.c

0 Answers  


What are the benefits of c++?

0 Answers  


Categories