Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Calling another program in C++ and directing the program flow

Reply
Thread Tools

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

 
 
utab
Guest
Posts: n/a
 
      04-24-2006
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.

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      04-24-2006
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.

 
Reply With Quote
 
 
 
 
utab
Guest
Posts: n/a
 
      04-24-2006
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)'

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      04-24-2006
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.

 
Reply With Quote
 
utab
Guest
Posts: n/a
 
      04-24-2006
bash: syntax error near unexpected token `('

Do I have check gnuplot or my shell

Thx

 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      04-24-2006
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!!!


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      04-24-2006
utab wrote:

> bash: syntax error near unexpected token `('
>
> Do I have check gnuplot or my shell


Your shell.

 
Reply With Quote
 
utab
Guest
Posts: n/a
 
      04-24-2006

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

 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      04-24-2006
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!!!
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      04-24-2006

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python, the Win32api, and directing input to game clients darkstorme Python 0 04-02-2009 10:49 PM
Query:difference between node flow and filter flow in java's I/O,system? Jack Dowson Java 0 05-07-2007 03:35 PM
directing Apache to another Tomcat Mike Java 0 09-03-2005 04:13 PM
Shortcut, And Re-Directing Of Shortcut Quest. ? Robert11 Computer Support 1 12-30-2004 03:13 PM
Re: Directing dynamically generated html content to word from ASP.NET Steve C. Orr [MVP, MCSD] ASP .Net 0 06-03-2004 06:36 AM



Advertisments