473,320 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
18 7484
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
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
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
bash: syntax error near unexpected token `('

Do I have check gnuplot or my shell

Thx

Apr 24 '06 #5
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
utab wrote:
bash: syntax error near unexpected token `('

Do I have check gnuplot or my shell


Your shell.

Apr 24 '06 #7

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

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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Albert Ahtenberg | last post by:
Hello, I had some bad experience with code organization and script functionality in writing my php based applications. And as the applications get bigger in scale it gets even worse. Therefore,...
4
by: Bruce W...1 | last post by:
A scripting newbie question... I'm trying to understand some code I found. This script conducts a poll and writes the results to a text file. The following statement is part of the source file. ...
9
by: Lil | last post by:
Hi Everyone! I've been trying to figure out this weird bug in my program. I have a python program that calls a C function that reads in a binary file into a buffer. In the C program, buffer is...
7
by: jacob navia | last post by:
Suppose that you want to know where your program has passed, i.e. the exact flow of the program. A brute force solution is to make an array of n lines, n being the number of lines in the...
1
by: Brett | last post by:
I'd like to have all of my documentation in one place. I use the following for documenting code: - attributes for certain types of documentation - use of the C# generated inline XML...
11
by: seattleboatguy | last post by:
I am trying to send a message from a visual c++ user-thread to the main window, so the main window can update text on the window to reflect what the user-thread is doing. I have tried 2 different...
8
by: lovecreatesbea... | last post by:
K&R 2, sec 2.4 says: If the variable in question is not automatic, the initialization is done once only, conceptually before the program starts executing, ... . "Non-automatic variables are...
6
by: Crooter | last post by:
Hello colleagues, Could anybody tell me if there are existing open-source solutions to extract the program tree using a program source code? I'm aware that GCC has program flow information and...
6
by: Sagari | last post by:
Greetings, Can someone suggest an efficient way of calling method whose name is passed in a variable? Given something like: class X: #... def a(self):
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.