0

hope to be fine... i'm trying write code that use execl within execl in C language but it's not working when i put the directory of file. the code is:

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/wait.h>
 int main(int argc,char* argv[]){
  int n,m,x;
  n=fork();
  if(n==0){
     execl("/bin/rm","rm /home/mazenas/Desktop/folder/","f.txt",NULL);
          }
  else{
     wait(&m);
     printf("end of Programm\n");
      }
  return 0;
  }

help me plaese!

Mazen AS
  • 9
  • 2

2 Answers2

5

The arguments of execl() is specifying the command-line arguments of the program to be executed.

execl("/bin/rm","rm /home/mazenas/Desktop/folder/","f.txt",NULL);

means to execute this:

"rm /home/mazenas/Desktop/folder/" "f.txt"

If you want to execute this command:

rm "/home/mazenas/Desktop/folder/f.txt"

You should write:

execl("/bin/rm", "rm", "/home/mazenas/Desktop/folder/f.txt", NULL);
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
3

I'm pretty sure you mean

 execl("/bin/rm","rm", "/home/mazenas/Desktop/folder/f.txt",NULL);

If rm checks its invocation name, then f.txt makes no sense and it complains; otherwise it's going to try to rm f.txt in the current directory rather than the given path.

Joshua
  • 40,822
  • 8
  • 72
  • 132