Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 19th, 2005, 08:40 PM
Joseph Suprenant
Guest
 
Posts: n/a
Default calling GNUPLOT from a c++ program

Hello all,
I have a C++ program, it does some calculations on things and then
prints out a file in the format in which GNUPLOT can use. So my question is
how would i call GNUPLOT from my C++ program. I know in some operating
systems you can do system("gnuplot"); But not with red hat 7.3. So could
some kind soul help me out? After it starts up GNUPLOT my program will
terminate.
Thanks



  #2  
Old July 19th, 2005, 08:40 PM
Moonlit
Guest
 
Posts: n/a
Default Re: calling GNUPLOT from a c++ program

Hi,

"Joseph Suprenant" <laclac01@yahoo.com> wrote in message
news:ARRmb.35086$%a2.6446@twister.nyroc.rr.com...[color=blue]
> Hello all,
> I have a C++ program, it does some calculations on things and then
> prints out a file in the format in which GNUPLOT can use. So my question[/color]
is[color=blue]
> how would i call GNUPLOT from my C++ program. I know in some operating
> systems you can do system("gnuplot"); But not with red hat 7.3. So could
> some kind soul help me out? After it starts up GNUPLOT my program will
> terminate.[/color]

Strange, that shouldn't happen. Did you try to intercept all signals? Likely
your program terminates because it gets a signal that it isn't expecting.


Here is some code:

void CCSFilt::SignalReloadFilters( int Dummy )
{
GoReloadFilters = true; // Reload on next alarm
signal( SIGUSR1, SignalReloadFilters );

}

signal( SIGUSR1, SignalReloadFilters );

Do this for all signals and see which one is sent.
[color=blue]
> Thanks
>
>
>[/color]
Regards, Ron AF Greve.



  #3  
Old July 19th, 2005, 08:40 PM
Joseph Suprenant
Guest
 
Posts: n/a
Default Re: calling GNUPLOT from a c++ program

are you talking to me?
I don't get it
"Moonlit" <alt.spam@jupiter.universe> wrote in message
news:3f9bf9bd$0$58713$e4fe514c@news.xs4all.nl...[color=blue]
> Hi,
>
> "Joseph Suprenant" <laclac01@yahoo.com> wrote in message
> news:ARRmb.35086$%a2.6446@twister.nyroc.rr.com...[color=green]
> > Hello all,
> > I have a C++ program, it does some calculations on things and then
> > prints out a file in the format in which GNUPLOT can use. So my[/color][/color]
question[color=blue]
> is[color=green]
> > how would i call GNUPLOT from my C++ program. I know in some operating
> > systems you can do system("gnuplot"); But not with red hat 7.3. So[/color][/color]
could[color=blue][color=green]
> > some kind soul help me out? After it starts up GNUPLOT my program will
> > terminate.[/color]
>
> Strange, that shouldn't happen. Did you try to intercept all signals?[/color]
Likely[color=blue]
> your program terminates because it gets a signal that it isn't expecting.
>
>
> Here is some code:
>
> void CCSFilt::SignalReloadFilters( int Dummy )
> {
> GoReloadFilters = true; // Reload on next alarm
> signal( SIGUSR1, SignalReloadFilters );
>
> }
>
> signal( SIGUSR1, SignalReloadFilters );
>
> Do this for all signals and see which one is sent.
>[color=green]
> > Thanks
> >
> >
> >[/color]
> Regards, Ron AF Greve.
>
>
>[/color]


  #4  
Old July 19th, 2005, 08:40 PM
Moonlit
Guest
 
Posts: n/a
Default Re: calling GNUPLOT from a c++ program

Hi,


"Joseph Suprenant" <laclac01@yahoo.com> wrote in message
news:NtTmb.82096$Hs.60115@twister.nyroc.rr.com...[color=blue]
> are you talking to me?[/color]

Yes.
[color=blue]
> I don't get it[/color]

What I meant is that your program shouldn't terminate when it uses the
system command. Likely that your program receives a signal. Some signals may
terminate (abort) your program when you do not intercept them. Use "man
signal" (I believe it is man 5 signal but am not sure) to see the signals
that might be sent.

Try to intercept them so they do not terminate your program. This you can do
with the following code.

void SignalCHLD( int Dummy )
{
signal( SIGCHLD, SignalCHLD );
fprintf( stderr, "SIGCHLD received" ); // Not actually allowed
normally, but will work most of the time.
}



signal( SIGCHLD, SignalCHLD ); // Do this for all possible signals!
system( "gnuplot" );

Do this for all signals and see what signals your program receives.

Regards, Ron AF Greve.






[color=blue]
> "Moonlit" <alt.spam@jupiter.universe> wrote in message
> news:3f9bf9bd$0$58713$e4fe514c@news.xs4all.nl...[color=green]
> > Hi,
> >
> > "Joseph Suprenant" <laclac01@yahoo.com> wrote in message
> > news:ARRmb.35086$%a2.6446@twister.nyroc.rr.com...[color=darkred]
> > > Hello all,
> > > I have a C++ program, it does some calculations on things and then
> > > prints out a file in the format in which GNUPLOT can use. So my[/color][/color]
> question[color=green]
> > is[color=darkred]
> > > how would i call GNUPLOT from my C++ program. I know in some[/color][/color][/color]
operating[color=blue][color=green][color=darkred]
> > > systems you can do system("gnuplot"); But not with red hat 7.3. So[/color][/color]
> could[color=green][color=darkred]
> > > some kind soul help me out? After it starts up GNUPLOT my program[/color][/color][/color]
will[color=blue][color=green][color=darkred]
> > > terminate.[/color]
> >
> > Strange, that shouldn't happen. Did you try to intercept all signals?[/color]
> Likely[color=green]
> > your program terminates because it gets a signal that it isn't[/color][/color]
expecting.[color=blue][color=green]
> >
> >
> > Here is some code:
> >
> > void CCSFilt::SignalReloadFilters( int Dummy )
> > {
> > GoReloadFilters = true; // Reload on next alarm
> > signal( SIGUSR1, SignalReloadFilters );
> >
> > }
> >
> > signal( SIGUSR1, SignalReloadFilters );
> >
> > Do this for all signals and see which one is sent.
> >[color=darkred]
> > > Thanks
> > >
> > >
> > >[/color]
> > Regards, Ron AF Greve.
> >
> >
> >[/color]
>
>[/color]


  #5  
Old July 19th, 2005, 08:40 PM
Joseph Suprenant
Guest
 
Posts: n/a
Default Re: calling GNUPLOT from a c++ program

well my original problem still exists how do i start GNUPLOT from my C++
program?

"Moonlit" <alt.spam@jupiter.universe> wrote in message
news:3f9c1514$0$58698$e4fe514c@news.xs4all.nl...[color=blue]
> Hi,
>
>
> "Joseph Suprenant" <laclac01@yahoo.com> wrote in message
> news:NtTmb.82096$Hs.60115@twister.nyroc.rr.com...[color=green]
> > are you talking to me?[/color]
>
> Yes.
>[color=green]
> > I don't get it[/color]
>
> What I meant is that your program shouldn't terminate when it uses the
> system command. Likely that your program receives a signal. Some signals[/color]
may[color=blue]
> terminate (abort) your program when you do not intercept them. Use "man
> signal" (I believe it is man 5 signal but am not sure) to see the signals
> that might be sent.
>
> Try to intercept them so they do not terminate your program. This you can[/color]
do[color=blue]
> with the following code.
>
> void SignalCHLD( int Dummy )
> {
> signal( SIGCHLD, SignalCHLD );
> fprintf( stderr, "SIGCHLD received" ); // Not actually allowed
> normally, but will work most of the time.
> }
>
>
>
> signal( SIGCHLD, SignalCHLD ); // Do this for all possible signals!
> system( "gnuplot" );
>
> Do this for all signals and see what signals your program receives.
>
> Regards, Ron AF Greve.
>
>
>
>
>
>
>[color=green]
> > "Moonlit" <alt.spam@jupiter.universe> wrote in message
> > news:3f9bf9bd$0$58713$e4fe514c@news.xs4all.nl...[color=darkred]
> > > Hi,
> > >
> > > "Joseph Suprenant" <laclac01@yahoo.com> wrote in message
> > > news:ARRmb.35086$%a2.6446@twister.nyroc.rr.com...
> > > > Hello all,
> > > > I have a C++ program, it does some calculations on things and[/color][/color][/color]
then[color=blue][color=green][color=darkred]
> > > > prints out a file in the format in which GNUPLOT can use. So my[/color]
> > question[color=darkred]
> > > is
> > > > how would i call GNUPLOT from my C++ program. I know in some[/color][/color]
> operating[color=green][color=darkred]
> > > > systems you can do system("gnuplot"); But not with red hat 7.3. So[/color]
> > could[color=darkred]
> > > > some kind soul help me out? After it starts up GNUPLOT my program[/color][/color]
> will[color=green][color=darkred]
> > > > terminate.
> > >
> > > Strange, that shouldn't happen. Did you try to intercept all signals?[/color]
> > Likely[color=darkred]
> > > your program terminates because it gets a signal that it isn't[/color][/color]
> expecting.[color=green][color=darkred]
> > >
> > >
> > > Here is some code:
> > >
> > > void CCSFilt::SignalReloadFilters( int Dummy )
> > > {
> > > GoReloadFilters = true; // Reload on next alarm
> > > signal( SIGUSR1, SignalReloadFilters );
> > >
> > > }
> > >
> > > signal( SIGUSR1, SignalReloadFilters );
> > >
> > > Do this for all signals and see which one is sent.
> > >
> > > > Thanks
> > > >
> > > >
> > > >
> > > Regards, Ron AF Greve.
> > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


  #6  
Old July 19th, 2005, 08:43 PM
Frank Schmitt
Guest
 
Posts: n/a
Default Re: calling GNUPLOT from a c++ program

"Joseph Suprenant" <laclac01@yahoo.com> writes:
[color=blue]
> well my original problem still exists how do i start GNUPLOT from my C++
> program?[/color]

Please don't top post.
Using system() is the right way to call an external program like gnuplot
from C++ - if it doesn't work properly, your problem must lie elsewhere.
Post some code.

regards
frank

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
  #7  
Old July 19th, 2005, 08:50 PM
Christian Gollwitzer
Guest
 
Posts: n/a
Default Re: calling GNUPLOT from a c++ program

Joseph Suprenant wrote:[color=blue]
> Hello all,
> I have a C++ program, it does some calculations on things and then
> prints out a file in the format in which GNUPLOT can use. So my question is
> how would i call GNUPLOT from my C++ program. I know in some operating
> systems you can do system("gnuplot");[/color]
system is useless since then you can't pass any commands to GNUplot
[color=blue]
> But not with red hat 7.3. So could
> some kind soul help me out? After it starts up GNUPLOT my program will
> terminate.[/color]

I've done this on Linux using the popen() function (which is not
standard C++, but POSIX-standard)

class GNUplot {
public:
GNUplot() throw(string);
~GNUplot();
void operator ()(const string& command);
// send any command to gnuplot
protected:
FILE *gnuplotpipe;
};

GNUplot::GNUplot() throw(string) {
gnuplotpipe=popen("gnuplot","w");
if (!gnuplotpipe) {
throw("Gnuplot not found !");
}
}

GNUplot::~GNUplot() {
fprintf(gnuplotpipe,"exit\n");
pclose(gnuplotpipe);
}

void GNUplot::operator() (const string& command) {
fprintf(gnuplotpipe,"%s\n",command.c_str());
fflush(gnuplotpipe);
// flush is necessary, nothing gets plotted else
};


You simply construct one object and invoke it with operator () like

GNUplot plotter;
plotter("plot sin(x)");

Note that GNUplot will be killed as soon as your program terminates. So
you need to wait for keystroke or similar, otherwise you will only see
short flashing of the graph. If you need that the graph window stays on
screen after your pragram fnished, then instead of "gnuplot" in the
constructor invoke "gnuplot -persist".



--
Vale !
Christianus Auriocus

 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles