Thread: running program with arguments (linux)

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    10

    running program with arguments (linux)

    Well, I wanted to know how I can run a program from a program, eg:

    int main()
    {
    char *prog;
    cout<<"Run: "; // --> g++ -o file1.cpp file2
    cin>>prog;

    [code to run prog with arguments]
    }

    I hope you understand what I mean.
    I tried system("g++ -o...") but that doesn't work. thanks

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    You use fork() and execve(and its derivatives) to run a program within a program.
    Fork creates a copy of the current process and you overwrite the content of the child by an execve call.


    [CODE]
    #include <unistd.h>
    // some other header files that I could not remember now.
    // Check man fork and man execve.

    main()
    {
    char *prog = "./myprog.o";
    char *parameter = "23";
    pid_t pid;
    int status;

    pid = fork();
    if (pid == 0) {
    execlp(prog, prog, parameter, NULL);
    }
    wait(&status);
    return 0;
    }

    It is a very primitive program but the general approach is this apart from the
    system function.
    Last edited by ozgulker; 01-28-2002 at 12:40 AM.
    The experimenter who does not know what he is looking for will not understand what he finds.
    - Claude Bernard

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    10
    thanks, that was exactly what I was searching for!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using getchar to keep program running
    By shadow1515 in forum C Programming
    Replies: 8
    Last Post: 05-25-2008, 02:33 AM
  2. Replies: 5
    Last Post: 04-10-2006, 03:03 PM
  3. running a program
    By task in forum C Programming
    Replies: 1
    Last Post: 05-31-2003, 08:01 AM
  4. Determaining if a program is running
    By WebmasterMattD in forum Linux Programming
    Replies: 4
    Last Post: 04-09-2002, 04:36 PM
  5. Why is my program running away?
    By badkitty in forum C++ Programming
    Replies: 4
    Last Post: 09-19-2001, 04:27 PM