Thread: system commands. deletion of file after program terminates

  1. #1
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    Arrow system commands. deletion of file after program terminates


    Is there a way to delete an executable after terminating itself?
    I want to delete my program after it has finished running.
    We are required to write a stack that when overloaded it restarts your machine and on reboot it reloads the data and deletes itself out of the startup folder? I keep getting access violations when trying to delete it while it is still running. Is there a command I can run to execute the delete after the program has completed running? Below is the error in the quote and also a selection from my code.
    Thanks for any help this provides.
    X
    C:\Documents and Settings\All Users\Start Menu\Programs\Startup\Stack Array.exe
    Access is denied.
    Code:
    void stacked::read()              
    {
    	system("del \"C:\\Documents and Settings\\All Users\\Start Menu\\Programs\\Startup\\Stack Array.exe\"");
    
    	ifstream instream("C:/overflow.dat");
    	
    	if(instream)
    	{		
    		dirtball();
    
    		for(int x=0;x<=SIZE-1;x++)
    		{
    			instream>>dataPtr[x];
    		}
    		instream>>leftDepth;
    		instream>>rightDepth;
    		instream>>nummies;
    	}		
    	display();
    	cout<<"Press any key"<<endl;
    	getch();
    
    	instream.close();
    }
    Last edited by xviddivxoggmp3; 11-18-2003 at 11:17 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I keep getting access violations when trying to delete it while it is still running...


    No say? What kind of OS would impose such a ridiculous constraint?


    >> We are required to write a stack that when overloaded it restarts your machine and on reboot it reloads the data and deletes itself out of the startup folder...


    Hmm, hope you're not planning on writing 3rd-party libraries any time soon ("Can't explain it Phil, I just pushed a value on the stack and the damn machine rebooted!").
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    I'm not the one that made the rules of the program, I'm just trying to get it to work.

    Do you know how to get a system command like delete file to work after the .exe calling it has ended?

    possibly like a delayed system command for a few seconds after program termination.

    or is there a way to transfer control to the original program after it is booted from the start menu. That would allow me to get around the access violation.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  4. #4
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    if anybody is looking for the answer i did it with


    execl
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    Try to escape the whitespace. Like this:

    system("del \"C:\\Documents\ and\ Settings\\All\ Users\\Start\ Menu\\Programs\\Startup\\Stack\ Array.exe\"");

    Another option is:

    unlink("C:\\Documents\ and\ Settings\\All\ Users\\Start\ Menu\\Programs\\Startup\\Stack\ Array.exe");
    Success is a two word story, "WORK" works.

  6. #6
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    i've never seen unlink?
    what does it do?
    is it a function?
    is there a webpage for details on it?

    is this what you are talking about?
    The _unlink function deletes the file specified by filename. _wunlink is a wide-character version of _unlink; the filename argument to _wunlink is a wide-character string. These functions behave identically otherwise.
    I found this on msdn.com

    can this function you are mentioning delete the program while it is running? e.g. load the running program in ram and delete the program from the rom, after execution it is no more?

    if not what I have working now is the way to go.
    I transfered control to another exe, then executed a no return delete.
    Last edited by xviddivxoggmp3; 11-21-2003 at 03:34 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    Success is a two word story, "WORK" works.

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    pratip, he is asking how the executable can delete itself. That is still not going to work because the OS will not allow the file to be deleted when it is still running.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    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);
    }
    Success is a two word story, "WORK" works.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Congratulations on finding out that windows and unix/linux are different. The original question was for a windows machine.

    Now for many extra bonus points, tell us how unix/linux manages to achieve this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    Now for many extra bonus points, tell us how unix/linux manages to achieve this.
    Salem, I don't know the answer. Can you please tell me, how?
    Success is a two word story, "WORK" works.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Salem, I don't know the answer.
    He didn't expect you to. "Bonus point" questions usually mean you need to do extra work such as search the net and read books until you find the answer. Rest assured that you can learn a lot about Unix/Linux in the process, which is always a good thing.
    My best code is written with the delete key.

  13. #13
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    Thanks Prelude for the suggestion. I found this link while searching which may solve the problem of deleting the executable file on exit.
    Success is a two word story, "WORK" works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. Replies: 2
    Last Post: 07-27-2007, 12:48 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM