469,270 Members | 1,732 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

gnuplot and system()

I'm trying to use the system() in a C++ program to execute gnuplot on
a file of data on OSX and have the graph pop up but so far the only
thing I've managed to do is get gnuplot to open.

as I minor test I did (unrelated stuff excised)

#include <stdlib.h>
int system(const char *string);
system("gnuplot");
system("plot sin(x)");
and the only thing that happened was gnuplot opening in terminal. For
that matter typing

plot sin(x) doesn't do anything even if it is typed manually into
terminal with gnuplot started. The graph only pops up if I enter it
directly into the console that opens when clicking on gnuplot in
finder. Anyway to fix this or more easily load a file to gnuplot from
the other program?

Jun 25 '07 #1
2 6323
Blah wrote:
I'm trying to use the system() in a C++ program to execute gnuplot on
a file of data on OSX and have the graph pop up but so far the only
thing I've managed to do is get gnuplot to open.

as I minor test I did (unrelated stuff excised)

#include <stdlib.h>
int system(const char *string);
system("gnuplot");
system("plot sin(x)");
and the only thing that happened was gnuplot opening in terminal. For
that matter typing

plot sin(x) doesn't do anything even if it is typed manually into
terminal with gnuplot started. The graph only pops up if I enter it
directly into the console that opens when clicking on gnuplot in
finder. Anyway to fix this or more easily load a file to gnuplot from
the other program?
How system works is implementation-defined, so how to use it will depend on
your compiler. But to give you a hint, this works on my system:

std::ofstream file("file");
file << "plot sin(x)\n";
file.close();
std::system("gnuplot file");

If you want a more system specific solution, not creating a file, try:

#include <cstdio>
std::FILE *f = popen("gnuplot", "w");
std::fputs("plot sin(x)\n", f);
std::fflush(f);
....
std::fclose(f);

popen() is, however not part of the C++ standard library, so the discussion
of it would be off topic here.

--
rbh
Jun 25 '07 #2
In article <11**********************@g37g2000prf.googlegroups .com>,
Blah <ci*******@yahoo.comwrote:
>I'm trying to use the system() in a C++ program to execute gnuplot on
a file of data on OSX and have the graph pop up but so far the only
thing I've managed to do is get gnuplot to open.

as I minor test I did (unrelated stuff excised)

#include <stdlib.h>
int system(const char *string);
system("gnuplot");
system("plot sin(x)");
This is not even close to correct.
You don't want system();
you want popen(), followed by a series of fprintf() calls to pass
in commands.
..

--
Ethan A Merritt
Jun 25 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Rama Calaga | last post: by
6 posts views Thread by Joseph Suprenant | last post: by
4 posts views Thread by Alexander Stippler | last post: by
3 posts views Thread by Bernhard Hidding | last post: by
1 post views Thread by Nicola Kaiser | last post: by
reply views Thread by bwaha | last post: by
reply views Thread by Titi Anggono | last post: by
3 posts views Thread by Peter Beattie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.