By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,346 Members | 2,318 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,346 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Apr 24 '06 #1
Share this Question
Share on Google+
18 Replies


P: n/a
utab wrote:
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.

Apr 24 '06 #2

P: n/a
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)'

Apr 24 '06 #3

P: n/a
utab wrote:
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.

Apr 24 '06 #4

P: n/a
bash: syntax error near unexpected token `('

Do I have check gnuplot or my shell

Thx

Apr 24 '06 #5

P: n/a
utab wrote:
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.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 24 '06 #6

P: n/a
utab wrote:
bash: syntax error near unexpected token `('

Do I have check gnuplot or my shell


Your shell.

Apr 24 '06 #7

P: n/a

Phlip wrote:
utab wrote:
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.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


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

Apr 24 '06 #8

P: n/a
utab wrote:
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.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 24 '06 #9

P: n/a

utab wrote:
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.

Apr 24 '06 #10

P: n/a
utab wrote:
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).

Apr 24 '06 #11

P: n/a
Markus Schoder wrote:
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.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 24 '06 #12

P: n/a
Phlip wrote:
Markus Schoder wrote:
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.


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

Apr 24 '06 #13

P: n/a
Markus Schoder wrote:
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.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 24 '06 #14

P: n/a
Phlip wrote:
Markus Schoder wrote:
gnuplot reads commands from stdin providing them through popen seems to
work.
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.
--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


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

Apr 24 '06 #15

P: n/a
Markus Schoder wrote:
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.
http://www.greencheese.org/ZeekLand <-- NOT a blog!!! 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...

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 24 '06 #16

P: n/a

Phlip wrote:

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.

Apr 24 '06 #17

P: n/a
Noah Roberts wrote:
Phlip wrote:

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.


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

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

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 24 '06 #18

P: n/a

Markus Schoder wrote:
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?

Apr 27 '06 #19

This discussion thread is closed

Replies have been disabled for this discussion.