simple program on execv() function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • martin paul
    New Member
    • Jan 2007
    • 15

    simple program on execv() function

    Sir please consider the following....
    I would like to copy contents from one file (mart1.c) to another file (mart2.c) using execv() function (using command line arguments).....
    The command line argument string is "cp mart1.c mart2.c"
    Consider the following code..
    int main (int argc, char *argv[])
    {
    char c[]="cp mart1.c mart2.c";
    argv[0]=strtok(c," " );
    argv[1]=strtok(0," " );
    argv[2]=strtok(0," ");
    execv("/bin/cp","cp",argv );
    }
    The contents are not getting copied when I run this code...Is there mistake in the code..Kindly correct the above code...
    Thank you..
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    I have moved this thread to the C forum. Hopefully you will find help there.

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      Hi,
      Code:
      int main (int argc, char *argv[])
      {
      char c[]="cp mart1.c mart2.c";
      //argv[0]=strtok(c," " ); //Instead of assigning do strcpy
      strcpy(argv[0],strtok(c," " ));
      argv[1]=strtok(0," " );
      argv[2]=strtok(0," ");
      execv("/bin/cp","cp",argv);
      }
      Replace assignment with strcpy and then try it...that will solve the issue.

      Thanks
      Raghuram

      Comment

      • martin paul
        New Member
        • Jan 2007
        • 15

        #4
        Originally posted by gpraghuram
        Hi,
        Code:
        int main (int argc, char *argv[])
        {
        char c[]="cp mart1.c mart2.c";
        //argv[0]=strtok(c," " ); //Instead of assigning do strcpy
        strcpy(argv[0],strtok(c," " ));
        argv[1]=strtok(0," " );
        argv[2]=strtok(0," ");
        execv("/bin/cp","cp",argv);
        }
        Replace assignment with strcpy and then try it...that will solve the issue.

        Thanks
        Raghuram
        Thank you Sir....
        Sir could u please tell me the difference between copying and assigning...
        Thank you...

        Comment

        Working...