how to call an external program?

S

Sanyi Benczik

Is there a standard way in C++ to call an external program or is this
platform dependent?

If it is platform dependent, can somebody drop a line what to use or
where to look for info for GNU C++ on RedHat? I am doing some numerical
calculations and need to invoke an external plotting program (gnuplot)
and return to the numerics.

Sorry if I posted twice.

Thanks,
Sanyi
 
B

Brian Kerrick Nickel

Is there a standard way in C++ to call an external program or is this
platform dependent?

If it is platform dependent, can somebody drop a line what to use or
where to look for info for GNU C++ on RedHat? I am doing some numerical
calculations and need to invoke an external plotting program (gnuplot)
and return to the numerics.

I don't know how platform independent this is, but <stdio.h> contains:
FILE *popen(const char *command, const char *type);
This allows you to open a program and either read stdin ("r") or write stdout
("w") in the same manner as fopen.

There is probably a better solution, but this is the first thing that came
to mind.

-Brian
 
C

Chris Mantoulidis

Sanyi Benczik said:
Is there a standard way in C++ to call an external program or is this
platform dependent?

If it is platform dependent, can somebody drop a line what to use or
where to look for info for GNU C++ on RedHat? I am doing some numerical
calculations and need to invoke an external plotting program (gnuplot)
and return to the numerics.

Sorry if I posted twice.

Thanks,
Sanyi

I am not sure, but maybe System(char *) might seem helpful... Then you
can pipe the output to your program or something.
 
A

Andy

#include <stdlib.h>

int system(const char* progCmdLin); // standard, fairly limited in
capability

Depending how you want to design your program, you have a few options.

Popen is definitely one.

It might make sense to let the gnuplot program come up separately,
display your graph and stay there while the processing logic
continues.

Then you can spawn a process or a thread asynchronously to do that.
Lookup how to use system APIs. Look for the execle, execlp, execve,
execvp APIs plus the fork API.
 
M

Matej Pivoluska

Sanyi said:
Is there a standard way in C++ to call an external program or is this
platform dependent?

If it is platform dependent, can somebody drop a line what to use or
where to look for info for GNU C++ on RedHat? I am doing some numerical
calculations and need to invoke an external plotting program (gnuplot)
and return to the numerics.

I believe that gnuplot has C and C++ interfaces. So you don't need to call
any extrenal program, just include some header files initialize it and link
with right libraries.

(At RH you probbably need to install gnuplot-devel RPM package)

See www.gnuplot.info

And then write me your experiences with this, please :).
 
T

Thomas Matthews

Brian said:
I don't know how platform independent this is, but <stdio.h> contains:
FILE *popen(const char *command, const char *type);
This allows you to open a program and either read stdin ("r") or write stdout
("w") in the same manner as fopen.

There is probably a better solution, but this is the first thing that came
to mind.

-Brian
FYI, "popen" is not a standard C++ function. I doesn't exist in at
least two of my compilers.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
T

Thomas Matthews

Jeff said:
man system 3
Please do not reply with platform specific information
and also don't top-post.

Microsoft Windows 2000 [Version 5.00.2195]
(C) Copyright 1985-2000 Microsoft Corp.

C:\>man system 3
No manual entry for system
What manual page do you want from section 3?

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
T

Thomas Matthews

Chris said:
I am not sure, but maybe System(char *) might seem helpful... Then you
can pipe the output to your program or something.

BTW, the function is "system". The C++ language is case-sensitive,
"system" and "System" are different identifiers.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
J

Jeff Schwab

Thomas said:
Please do not reply with platform specific information

Sorry; my mistake!

and also don't top-post.

Top-posting has gotten a bad rap. I'm bringing it back into fashion! :)
Seriously, I think it's often appropriate, but I guess that's sort of
a dead horse... <grumble/><grumble/>

</ot>
 
C

Chris Mantoulidis

BTW, the function is "system". The C++ language is case-sensitive,
"system" and "System" are different identifiers.

I know C++ is case-sensitive (d'oh)... I just haven't used
sytem(char*) for a long time. I don't really need it... I have other
alternatives ;)

cmad
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top