Program to call other programs
I am writing a program that will solve a type of min. spanning tree problem. i have 2 different algorithms that I've got开发者_StackOverflow社区ten working in two separate .cpp files i've named kruskels.cpp and prims.cpp.
my question is this:
each file takes the following command line to run it . time ./FILENAME INPUTFILE FACTOR
i would like to make a program that, depending on what inputfile is entered, will run either kruskels.cpp or prims.cpp. how can i do this?
this program must pass those command line arguments to kruskels or prims. each file (kruskels.cpp and prims.cpp) are designed to be run using those command line arugments (so they take in INPUTFILE and FACTOR as variables to do file io).
this should be for c++.
You can call external programs using the system
function.
However, it would be much better to build your Kruskal and Prim solvers in a modular way as classes, and instantiate the appropriate class from your main
, according to the input. For this you'll link kruskels.cpp, prims.cpp and your main.cpp into a single executable.
The standard way is to use system()
. You might also want to look up popen()
(or, on Windows, _popen()
).
Edit: My assumption was that you have two executables and (critical point) want to keep them as separate executables. In that case, using system is pretty straightforward. For example, you could do something like:
std::stringstream buffer;
if (use_Kruskals)
buffer << "Kruskals " << argv[1] << argv[2];
else
buffer << "Prims " << argv[1] << argv[2];
system(buffer.str().c_str());
Depending on what you're doing (and as Eli pointed out) you might want to create a single executable, with your implementations of Prim's and Kruskal's methods in that one executable instead. Without seeing your code for them, it's impossible to guess how much work this would be though.
If you need your top program to regain control after executing one of your two child programs, use system() or popen(), if you don't need it then you can use execve()
精彩评论