Calling another program in C++ and directing the program flow

U

utab

Dear all,

I am making a system call to the well known Gnuplot with

system("gnuplot");

gnuplot opens if I only supply this command but I would like to pipe
that command line in my C++ program. For example I want to run a very
simple command like "plot sin(x)". So after running another program,
what is the way to pipe that with some commands from inside the c++
program.

Or should I ask this in Gnuplot forums?

Thx for the replies in advance.
 
R

Rolf Magnus

utab said:
Dear all,

I am making a system call to the well known Gnuplot with

system("gnuplot");

gnuplot opens if I only supply this command but I would like to pipe
that command line in my C++ program. For example I want to run a very
simple command like "plot sin(x)". So after running another program,
what is the way to pipe that with some commands from inside the c++
program.

system() is supposed to supply the given command line to the system's shell
if possible. So the command that you want to execute should be given to
system() just like you would type it in the shell.
Or should I ask this in Gnuplot forums?

This is not gnuplot specific, so no.
 
U

utab

like this

system("gnuplot plot sin(x)");

compiles

but gives errors:

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `gnuplot plot sin(x)'
 
R

Rolf Magnus

utab said:
like this

system("gnuplot plot sin(x)");

compiles

but gives errors:

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `gnuplot plot sin(x)'

Your shell interprets the '(' in a way that you don't want. What happens if
you enter

gnuplot plot sin(x)

directly into your shell? I'd guess the same. Find out what the correct
command line has to look like.
 
P

Phlip

utab said:
bash: syntax error near unexpected token `('

Do I have check gnuplot or my shell

What did you do? Call system("source.cpp")?

That error looks like bash saw .cpp source.
 
U

utab

Phlip said:
What did you do? Call system("source.cpp")?

That error looks like bash saw .cpp source.

I did

like this

system("gnuplot plot sin(x)");

compiles

but gives errors:

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `gnuplot plot sin(x)'
 
P

Phlip

utab said:
system("gnuplot plot sin(x)");

compiles

but gives errors:

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `gnuplot plot sin(x)'

This is now a question for a bash forum, so if the following tips are wrong,
please complain there and not here.

Bash probably interprets the (), so you can escape them:

system("gnuplot plot sin\\(x\\)");
system("gnuplot \"plot sin(x)\"");

But that won't work. Bash will like them but gnuplot won't. (And the sin(x)
most certainly will not convert to a call to your program's sin()
function.) Write a file with your data and commands in it, and pass the
filename on Gnuplot's command line. Get Gnuplot working on a command line,
first, before coming back to C++. And consider driving Gnuplot with a
softer language, such as Ruby. There are a couple of Gnuplot wrapper
classes available there.
 
N

Noah Roberts

utab said:
Dear all,

I am making a system call to the well known Gnuplot with

system("gnuplot");

gnuplot opens if I only supply this command but I would like to pipe
that command line in my C++ program. For example I want to run a very
simple command like "plot sin(x)". So after running another program,
what is the way to pipe that with some commands from inside the c++
program.

What you want to do is very specific to OS. If you want to interact
with an external program you have to use the posix function exec (or
one of its friends) and set up your stdin/out correctly. It's easy but
somewhat complex and is off topic. Ask in a linux/unix programmer
ground and/or google for the unix programming faq.

system() will run and block until the program is done (I believe, I
don't use it because it is usually too limited) and get the output but
won't let you interact with it...you can't "pipe" commands to your
external program with system().

There are also utility classes in frameworks such as Qt.
 
M

Markus Schoder

utab said:
Dear all,

I am making a system call to the well known Gnuplot with

system("gnuplot");

gnuplot opens if I only supply this command but I would like to pipe
that command line in my C++ program. For example I want to run a very
simple command like "plot sin(x)". So after running another program,
what is the way to pipe that with some commands from inside the c++
program.

You want popen(). Like in the following example:

#include <cerrno>
#include <cstdio>
#include <iostream>

using namespace std;

int
main()
{
FILE *pg = popen("/usr/bin/gnuplot", "w");
if(!pg)
{
cerr << "popen failed: " << strerror(errno) << '\n';
return 1;
}
fputs("plot sin(x)\n", pg);
fflush(pg);
sleep(3);
pclose(pg);
}

Or should I ask this in Gnuplot forums?

comp.os.linux.development.apps would probably be the right place (I
guess you use Linux).
 
P

Phlip

Markus said:
You want popen(). Like in the following example:

The OP doesn't need popen() (which I suspect is off-topic). Gnuplot has both
an interactive mode and a batch mode. The OP needs to write a batch as a
text file, and pass this to Gnuplot. Abusing Gnuplot via popen() would
solve the wrong problem, add might accidentally work, leaving the OP with a
lot of fragile complexity.
 
M

Markus Schoder

Phlip said:
The OP doesn't need popen() (which I suspect is off-topic). Gnuplot has both
an interactive mode and a batch mode. The OP needs to write a batch as a
text file, and pass this to Gnuplot. Abusing Gnuplot via popen() would
solve the wrong problem, add might accidentally work, leaving the OP with a
lot of fragile complexity.

gnuplot reads commands from stdin providing them through popen seems to
work. If you want to make sure interactive mode does not do anything
unwanted use

popen("gnuplot /dev/fd/0", "w")

to put gnuplot in batch mode but I doubt there will be any noticeable
difference. Anyway popen or a similar mechanism is needed to prevent
gnuplot from exiting real quick (batch mode with a regular file suffers
from this problem as well).
 
P

Phlip

Markus said:
gnuplot reads commands from stdin providing them through popen seems to
work.

This is example why off-topic threads damage USENET.

Write a batch of gnuplot commands, including one command to output your plot
in a portable format, such as PNG. Then display this format, independent of
gnuplot, in a viewer such as a web browser.

I'm telling the OP to do it like that because I did it like that in my last
several projects that used Gnuplot, and I disregarded the popen() option
each time.
 
M

Markus Schoder

Phlip said:
This is example why off-topic threads damage USENET.

OMG! I hope the fabric of the universe was not damaged as well...

You also managed to carefully omit the part were I show how you can
safely interact with gnuplot in batch mode using popen.

Also wasn't it you who proposed not long ago that platform neutral APIs
like POSIX should not be considered off-topic? Well, popen is POSIX.
Write a batch of gnuplot commands, including one command to output your plot
in a portable format, such as PNG. Then display this format, independent of
gnuplot, in a viewer such as a web browser.

I'm telling the OP to do it like that because I did it like that in my last
several projects that used Gnuplot, and I disregarded the popen() option
each time.

That may or may not be helpful to the OP. It certainly does not answer
his very specific question.

You might want to check this link it currently points to a placeholder
page chock full of advertising.
 
P

Phlip

Markus said:
Also wasn't it you who proposed not long ago that platform neutral APIs
like POSIX should not be considered off-topic?

No.

The distinction is between high-level discussions of various programming
topics between regulars, and helping newbies. Help them with a reply that is
as detailed, accurate, and on-topic as you can, and direct them elsewhere.

As I pointed out many years ago, and someone reminded me not long ago,
off-topic answers cannot easily be reviewed by others.
Well, popen is POSIX.

Safetly interacting with gnuplot via popen is high-risk and fragile. The OP
needs a simple solution...
That may or may not be helpful to the OP. It certainly does not answer
his very specific question.

"Hi. How do I shoot my foot off with this machine gun?"

Here, use a plinker.
You might want to check this link it currently points to a placeholder
page chock full of advertising.

Jeeze good free help is hard to find. My bro Peter Merel, who runs that
site, seems to have had his license stolen by a robot...
 
N

Noah Roberts

Phlip said:
I'm telling the OP to do it like that because I did it like that in my last
several projects that used Gnuplot, and I disregarded the popen() option
each time.

Well, since _you_ do it that way and _you_ disregarded popen() then we
should all shut-up and let _you_ tell the OP how it should be done.
 
P

Phlip

Noah said:
Well, since _you_ do it that way and _you_ disregarded popen() then we
should all shut-up and let _you_ tell the OP how it should be done.

Yes. Anyone else with Gnuplot experience, raise their hands.

BTW the Ruby library I suggested uses the equivalent of popen... ;-)
 
D

Diego Martins

Markus said:
You want popen(). Like in the following example: [snip]
fputs("plot sin(x)\n", pg);
fflush(pg);
sleep(3);
pclose(pg);
}

why are you using sleep(3) there?
 

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