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

P

Peter Nolan

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.
 
P

Pete

Peter said:
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
 
P

Peter Nolan

Pete said:
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) ;
}
 
K

Kevin Goodsell

Peter said:
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
 
E

E. Robert Tisdale

Kevin said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top