Hongwei Wang <hw...@ewl.uky.edu> says
------------------------------------------------
I am trying to write a CGI script with c++ and embedded gnuplot
command to draw a real time curve. Could you tell me how to embed this
gnuplot command in C++? Or maybe you can send me a very simple code with
embedded gnuplot.
-----------------------------------------------
Hi, Hongwei,
My CGI script calling gnuplot is very simple. If you write it in Perl
(I don't know C++), the following should work
#!/usr/local/bin/perl -w
require "cgi-lib.pl"; #use cgi.pm if you want the new version
&ReadParse; #but then you need change this and something
else
$userfunc=$in{'usrfuncfromHTML'};
open GPLOT, " | gnuplot" or die "Can't open gnuplot: $!\n";
print GPLOT, "plot $usrfunc";
#or something like print GPLOT, "set output 'myoutfile'";
#then print GPLOT, "plot $usrfunc";
close GPLOT;
In fact I wrote mine in csh because my script made quite a few calls to
launch other applications in the file system. To pass commands to
gnuplot in csh (or any unix shell script), I use here documents like
this:
gnuplot <<END
set output "myoutfile"
plot $1
END
supposing $1 is a command line argument containing a user inputted
function.
So, basically I can see two ways to pass commands to gnuplot, open a
pipe as in Perl and use here documents. There may be other ways.
Good luck.
Yong
At the unix prompt, do a ppmtogif -o "mygif.gif" myfile.ppm (I can't
remember the syntax for ppmtogif). If you don't have ppmtogif, do a Net
search on Netpbm at Yahoo or AltaVista.
If you have more questions, let me know.
Yong
Make progress already? Besides what I said in another news group with
you, here is a place you can find something useful, including "how do I
call gnuplot from my own programms" in an Unix example:
http://www.uni-karlsruhe.de/~ig25/gnuplot-faq/
or
ftp://rtfm.mit.edu/pub/usenet/news.answers/graphics/gnuplot-faq
Seems you are still using v3.5 rather than v3.6beta. Compiled binary
v3.6beta are available for different OS and work fine from my
experience. To get gif option, need gd1.2+v3.6 and compile them by
yourself. The above mentioned FAQ tells all.
Sorry, don't try mail, please
(ppmtogif ./tmp/$tmpvar.ppm1 | giftrans -t '#FFFFFF' -o ./tmp/$tmpvar.
gif1) >& /dev/null
After I completely switch to gnuplot 3.6beta, this needs to be
re-written.
Yong
Hongwei says-----------------------------
> Thank you , Yong. I got your message in my email. But I don't know why you
> got msg back. I have got some idea on how to write C++ calling
> gnuplot.
The easiest way I know to talk to gnuplot with C++ is to use a named
pipe (works in UNIX, not sure about it in win95 etc). Actually you are
using C commands but they work in C++.
Example which works with gcc:
#include <stdio>
FILE* pipeVar;
pipeVar = popen("gnuplot","w"); /* opens writeable one-way pipe to
gnuplot. If not in path, errors
will occur. Return val of popen
will be NULL if error occurs.
see popen man page for details */
fprintf(pipeVar, "plot sin(x)\n"); /* sends command to gnuplot
Note: fprintf must be used,
cout won't work */
/* You can also send a variable containing the command instead of the
quoted string I show above, but it must be a char* string */
fflush(pipeVar); /* pipes are buffered, so flush buffer after you are
finished */
pclose(pipeVar); // close pipe when you are finished with your program
Hope that helps,
David
+--------------------------------------------------------------------+
| David Ishee |
| MS grad student, Mechanical Engineering dm...@ra.msstate.edu |
| Mississippi State University |
| |
+------------- http://www2.msstate.edu/~dmi1/index.html -------------+
Congratulation! You've made it work. Wish your gif option works well.
> Now I use system call in C++. You can write a script called "work.gnu"
> then you do this in C++:
> system("gnuplot work.gnu");
> I think it's much easier to do.
Yeah, sure tell him the EASY way! What's this world coming to?
--
Do you mean it is too easy to believe? No surprise to me. :-)