Help With Command Line Argument

Oct 11, 2015 at 8:01am
Hi There,
I have to write a program which takes "ls" command (that displays the list of files in linux) as a command line argument..
I don't have any idea .. how to execute that.. Please Help Me.. Stuck at the following program
Thanks In Advance :)
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <unistd.h>
using namespace std;
int main (int argc, char *argv[])
{
    int ret;
    cout<<"Displaying the list of files using ls as a command line argument\n";
    // I don't know what to do here
    ret= execl("/usr/bin/argv","list of files,o",NULL);
    if (ret == -1 )
        cout<<"failed\n";
    return 0;
}
Last edited on Oct 11, 2015 at 8:02am
Oct 11, 2015 at 10:16am
Take a look at this: http://pubs.opengroup.org/onlinepubs/007908799/xsh/execl.html

You'll see that execv is more suitable as it takes argv that you have, and the program name that you can supply.

So you call that function with the args. If you aren't sure where ls, run "which ls".
Topic archived. No new replies allowed.