View Single Post

  #2 (permalink)  
Old 10-14-2005, 09:35 AM
monk's Avatar
monk monk is offline
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 482
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
Default

I suggest to use C API that removes file. Here is Linux GCC API (code) to remove file:
Code:
#include<stdio.h>
#include <unistd.h> /* API to remove file */

int main(){
        if ( unlink("/tmp/file.tmp") == 0 ){
                printf("File deleted\n");
        }
        else{
                printf("Error deleting file\n");
        }
}
You need to use unlink("/path/to/file"); API -- unlink deletes a name from the filesystem. If that name was the last link to a file and no processes have the file open the file is deleted and the space it was using is made available for reuse.

However if you wish to use call shell command like rm then you need to use system() API - execute a shell command.

Code:
#include<stdio.h>
#include <stdlib.h> /* system() */

int main(){
     if ( system("rm -f /tmp/file.tmp") == -1 ){
            printf("Deleted\n");
     }
    else {
            printf("Cannot delete or call rm command");
    }
}
Use any one of the code. For more info read man pages
Code:
man 2 unlink 
man system
Reply With Quote