PN: Sample code to call another program/command from inside a C++ program

Discussion in 'C++' started by Peter Nolan, Apr 4, 2004.

  1. Peter Nolan

    Peter Nolan Guest

    Hi All,
    I have written some software that performs ETL processing to load data
    warehouses. Each program accepts a set of parameters and returns 0 or
    1 to the win/unix shell to indicate success or failure. Currently it
    is run as a set of commands that calls each program and then returns
    the return code and stops if the program has failed.

    I'm interested in enhancing the product by being able to call these
    programs from within another C++ program. This way I could create a
    table of programs to run and parameters to use and then the calling
    program could simply read the table, call each program in turn, and
    stop if there was an error. I could then add a GUI to set up the table
    of programs to run with parameters to pass.

    The entry point to each program is the normal main argv argc and the
    parameters are just a character string....the current version runs on
    win2k/unix.

    If anyone has a snippet of sample code to show me how to do this I
    would very much appreciate it.

    Best Regards
    Peter Nolan.
     
    Peter Nolan, Apr 4, 2004
    #1
    1. Advertising

  2. Peter Nolan

    Pete Guest

    Re: Sample code to call another program/command from inside a C++ program

    Peter Nolan wrote:
    > Hi All,
    > I have written some software that performs ETL processing to load data
    > warehouses. Each program accepts a set of parameters and returns 0 or
    > 1 to the win/unix shell to indicate success or failure. Currently it
    > is run as a set of commands that calls each program and then returns
    > the return code and stops if the program has failed.
    >
    > I'm interested in enhancing the product by being able to call these
    > programs from within another C++ program. This way I could create a
    > table of programs to run and parameters to use and then the calling
    > program could simply read the table, call each program in turn, and
    > stop if there was an error. I could then add a GUI to set up the table
    > of programs to run with parameters to pass.
    >
    > The entry point to each program is the normal main argv argc and the
    > parameters are just a character string....the current version runs on
    > win2k/unix.
    >
    > If anyone has a snippet of sample code to show me how to do this I
    > would very much appreciate it.
    >
    > Best Regards
    > Peter Nolan.


    Try the std::system function.

    - Pete
     
    Pete, Apr 4, 2004
    #2
    1. Advertising

  3. Peter Nolan

    Peter Nolan Guest

    Re: Sample code to call another program/command from inside a C++ program

    "Pete" <> wrote in message news:<B9Vbc.14519$>...
    > > Best Regards
    > > Peter Nolan.

    >
    > Try the std::system function.
    >
    > - Pete


    Hi Pete,
    thanks for the pointer.....I wrote a short C++ program as follows.

    However, it looks to me that system will wait for the call to return
    before moving on to the next statement.

    What I am looking for is how I might go about starting several
    programs at once and running them concurrently. For example, I might
    have 100 files to load from various places to a staging area and then
    move that data forward to the DW. Today running a reasonable number of
    jobs and dependencies are managed in scripts....but I'd like to be
    able to manage running jobs at once and then detect that they have all
    completed ok before moving onto the next part of the batch....I've
    been reading up about spawn and things like that in VS.net but I don't
    seem to find the command that will let me start a command and then
    come back to see how it is going...I know it exists because I read
    about it in one of my reference books that is currently 5,000 miles
    away from me....but I can't remember what it was and can't find it in
    the vs.net library....

    Any tips most apperciated.. :)


    #include <process.h>

    int main(int argc, char* argv[])
    {

    int return_code ;

    return_code = 0 ;

    return_code = system( "type c:\\temp.txt2" );

    return (return_code) ;
    }
     
    Peter Nolan, Apr 8, 2004
    #3
  4. Re: Sample code to call another program/command from inside a C++program

    Peter Nolan wrote:
    > "Pete" <> wrote in message news:<B9Vbc.14519$>...
    >
    >>>Best Regards
    >>>Peter Nolan.

    >>
    >>Try the std::system function.
    >>
    >>- Pete

    >
    >
    > Hi Pete,
    > thanks for the pointer.....I wrote a short C++ program as follows.
    >
    > However, it looks to me that system will wait for the call to return
    > before moving on to the next statement.
    >
    > What I am looking for is how I might go about starting several
    > programs at once and running them concurrently.


    Basically there's no good way to do what you want using standard C++.
    Frankly, most uses of system() would be better replaced with
    system-specific functions that offer more control.

    The best suggestion I can offer is to look at what is provided by your
    execution environment, and direct your questions to a group that
    discusses programming for your particular system.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Apr 8, 2004
    #4
  5. Re: Sample code to call another program/command from inside a C++program

    Kevin Goodsell wrote:

    > Peter Nolan wrote:
    >
    >> Pete wrote:
    >>
    >>> Try the std::system function.

    >>
    >> thanks for the pointer.....I wrote a short C++ program as follows.
    >>
    >> However, it looks to me that system will wait for the call to return
    >> before moving on to the next statement.
    >>
    >> What I am looking for is how I might go about starting several
    >> programs at once and running them concurrently.

    >
    > Basically there's no good way to do what you want using standard C++.
    > Frankly, most uses of system() would be better replaced with
    > system-specific functions that offer more control.
    >
    > The best suggestion I can offer is to look at what is provided by your
    > execution environment, and direct your questions to a group that
    > discusses programming for your particular system.


    > cat main.cc

    #include <iostream>

    int main(int argc, char* argv[]) {
    std::system("xclock &");
    return 0;
    }

    works fine for me.
     
    E. Robert Tisdale, Apr 8, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nail
    Replies:
    0
    Views:
    1,578
  2. Jake
    Replies:
    0
    Views:
    604
  3. Tim Chase
    Replies:
    0
    Views:
    96
    Tim Chase
    Feb 16, 2014
  4. Terry Reedy
    Replies:
    0
    Views:
    106
    Terry Reedy
    Feb 16, 2014
  5. Tim Chase
    Replies:
    0
    Views:
    102
    Tim Chase
    Feb 16, 2014
Loading...

Share This Page