473,769 Members | 5,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
18 7530
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.d evelopment.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
4649
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, I am trying to build a general schema for data flow in a php/mysql application. What I has in mind is to design a three major units. To handle the input, processing and data access. Plus another unit to generate the output.
4
2314
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. The exact code is not important to my question so don't wrack your brain on this. if (isset($votingstep)) { function ShowTheStuff($item, $itemvoted, $graph_width, $graph_height) { $hector=count($itemvoted);$totalvotes=0;$in=0;$stepstr='';...
9
2230
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 allocated by calling malloc. The C program runs perfectly fine but when I use python to call the C function, it core dumps at malloc. I've tried multiple binary files with different sizes and the result is: if file size is < 20 bytes , works fine...
7
1853
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 function. At each line then, you increment a variable like this: // NRLINES is the number of lines in the program module int lines; int fn(int a)
1
2012
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 documentation - simple comments I also use Visual Paradigm for UML (class relationships), which integrates with VS.NET. I need something for general documentation of program flow. Any suggestions on what to use for this?
11
9173
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 approaches, and both fail. I don't know what to try next. +++++++++++++++++++++++++++++++++++++++++++++++++++++ APPROACH #1: (this approach makes the most sence to me) Configure the first parameter of the thread's PostMessage() to talk to the...
8
3122
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 initialized before the program starts executing." -- What does this mean? What is the name of the stage in which the mentioned initialization is performed? Compile-time or run-time? In the following snippet, variables b and c are defined at line 7...
6
2440
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 tree respectively for optimization, but I'm looking for something less complicated. An ideal case would be a small program based on a parser with YACC compatible grammar which extracts the program flow tree. Best regards,
6
2996
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
9586
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10043
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.