• 0

[C++] Help using exec call


Question

Im having a some problems using execlp in a c++ program I'm doing, I'm a n00b in Linux C++ programming.

The problem is when my app execute the line:

int ret = execlp ("ls", "ls", "-c", NULL);

My app execute the code until this line and then it just quit, I dont know why.

THX

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0
Why do you have "ls" twice? First one is the path of the program name and the subsequent arguments are, well, the arguments.

You're trying to run

ls ls -c

Wrong.

The first argument is the path of what to execute.

The second argument is the name you want the new process to spawn as.

Link to comment
Share on other sites

  • 0
Wrong.

The first argument is the path of what to execute.

The second argument is the name you want the new process to spawn as.

Well, unless I'm reading this wrong: http://www.die.net/doc/linux/man/man3/execlp.3.html

The const char *arg and subsequent ellipses in the execl, execlp, and execle functions can be thought of as arg0, arg1, ..., argn. Together they describe a list of one or more pointers to null-terminated strings that represent the argument list available to the executed program. The first argument, by convention, should point to the file name associated with the file being executed. The list of arguments must be terminated by a NULL pointer.
Link to comment
Share on other sites

  • 0
The const char *arg and subsequent ellipses in the execl, execlp, and execle functions can be thought of as arg0, arg1, ..., argn. Together they describe a list of one or more pointers to null-terminated strings that represent the argument list available to the executed program. The first argument, by convention, should point to the file name associated with the file being executed. The list of arguments must be terminated by a NULL pointer.
Thanks.
Link to comment
Share on other sites

  • 0
Thanks.

Somehow I just glazed over that part and it never sunk in....

But yeah, it makes sense as the first argument in argv is always the name of the program, but somehow since I ignored that I figured the first argument would be argv[0] :pinch:

You can tell I don't use execlp much, I use system() more....

*Goes to bed* :wacko:

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.