Execl() function Arugments Error

Hi there..
I've been assigned to make a program in c++, that have to run on linux and print n most recent commands in history. If I only type history command it works well. But I've been asked to print n most recent commands. After hell of tries I'm stuck at how to print n commands. How to pass the argument about printing n commands
1
2
3
4
if (command == "history")
        {
        	execl("/bin/history","history",0,0);
        }

Thanks in advance
Since the args are variadic, they must all be type char*. If you want to pass an argument, it must be text:

1
2
3
4
5
6
int n = numberToPrint;
if (command == "history") {
    char buf[20];
    sprintf(buf, "%d", n);
    execl("/bin/history","history", buf, (char*)0);
}

what does this do ?
char buf[20];
and in execl("/bin/history","history", buf, (char*)0); you're only passing the buf it self .. not any of it's index number..
please explain be how will this work because I've to explain this in viva.. I didn't get that..

Thank you very much @dhayden
It sounds like you haven't really programmed in C++ before.
char buf[20]; defines an array of 20 characters. execl's parameters are what we call "C strings" - strings that are arrays of characters terminating in ASCII zero.

sprintf(buf, "%d", n); calls the function sprintf and tells it to convert the number n into a C string and put the result in buf.

execl("/bin/history","history", buf, (char*)0); Calls execl() to execute the program. The third parameter is the C string that sprintf() stored in buf. For example, if n is 4 then buf will contain the string "4".

By the way n = numberToPrint; was just an example to indicate that you need to store the number that you want to print into n somehow.

One more bit that might not be obvious: If execl() succeeds, it will never return, meaning that your program will stop executing there.
Thank You So much.. you cleared my mind.. I can't tell in words how happy is I am by reading your such elaborated example.. thank you once again @dhayden
Topic archived. No new replies allowed.