views:

101

answers:

3

hello. I am writing a program that will solve a type of min. spanning tree problem. i have 2 different algorithms that I've gotten 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++.

+5  A: 

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.

Eli Bendersky
+2  A: 

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.

Jerry Coffin
forgive me. I've never heard of system(). could you give a link to a good description of how to use it or perhaps explain? do i write a separate program? how do i pass the command line arguments off then?
Evil
system() is indeed how you'd perform this, but look at Eli's response. You probably don't want to do this. Make two functions and conditionally call one or the other.
Stephen
each .cpp uses the same struct names and some of the same function names so i could keep things similar between the two. should i just rename them and then merge the two programs together?
Evil
@Evil: that's what namespaces are for.
Mike Seymour
A: 

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()

ggiroux