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

Discussion in 'C++' started by utab, Apr 24, 2006.

  1. utab

    utab Guest

    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.
     
    utab, Apr 24, 2006
    #1
    1. Advertising

  2. utab

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Apr 24, 2006
    #2
    1. Advertising

  3. utab

    utab Guest

    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)'
     
    utab, Apr 24, 2006
    #3
  4. utab

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Apr 24, 2006
    #4
  5. utab

    utab Guest

    bash: syntax error near unexpected token `('

    Do I have check gnuplot or my shell

    Thx
     
    utab, Apr 24, 2006
    #5
  6. utab

    Phlip Guest

    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!!!
     
    Phlip, Apr 24, 2006
    #6
  7. utab

    Rolf Magnus Guest

    utab wrote:

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


    Your shell.
     
    Rolf Magnus, Apr 24, 2006
    #7
  8. utab

    utab Guest

    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)'
     
    utab, Apr 24, 2006
    #8
  9. utab

    Phlip Guest

    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!!!
     
    Phlip, Apr 24, 2006
    #9
  10. utab

    Noah Roberts Guest

    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.
     
    Noah Roberts, Apr 24, 2006
    #10
  11. 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).
     
    Markus Schoder, Apr 24, 2006
    #11
  12. utab

    Phlip Guest

    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!!!
     
    Phlip, Apr 24, 2006
    #12
  13. 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).
     
    Markus Schoder, Apr 24, 2006
    #13
  14. utab

    Phlip Guest

    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!!!
     
    Phlip, Apr 24, 2006
    #14
  15. 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.
     
    Markus Schoder, Apr 24, 2006
    #15
  16. utab

    Phlip Guest

    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!!!
     
    Phlip, Apr 24, 2006
    #16
  17. utab

    Noah Roberts Guest

    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.
     
    Noah Roberts, Apr 24, 2006
    #17
  18. utab

    Phlip Guest

    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!!!
     
    Phlip, Apr 24, 2006
    #18
  19. 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?
     
    Diego Martins, Apr 27, 2006
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve C. Orr [MVP, MCSD]
    Replies:
    0
    Views:
    443
    Steve C. Orr [MVP, MCSD]
    Jun 3, 2004
  2. Andrew Bonney \(abweb\)

    Directing Domains

    Andrew Bonney \(abweb\), Nov 16, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    914
    Andrew Bonney \(abweb\)
    Nov 16, 2004
  3. Mike
    Replies:
    0
    Views:
    337
  4. Jack Dowson
    Replies:
    0
    Views:
    469
    Jack Dowson
    May 7, 2007
  5. darkstorme
    Replies:
    0
    Views:
    329
    darkstorme
    Apr 2, 2009
Loading...

Share This Page