Below is a sample code where I am deleting the running executable file. This works fine.
Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i;
int status;
char cmd[50];
printf("Trying to delete the running executable file: %s\n", argv[0]);
sprintf(cmd, "ls %s", argv[0]);
/*
In case of windows use:
sprintf(cmd, "dir %s", argv[0]);
*/
printf("Executing OS command: %s\n", cmd);
system(cmd);
status = unlink(argv[0]);
printf("unlink() returned: %d\n", status);
printf("Check at the OS level whether the file is deleted\n");
system(cmd);
for(i=0; i<10; i++)
{
printf("%d", i);
}
printf("\nExiting application with Status: %d\n", status);
}