By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,193 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,193 IT Pros & Developers. It's quick & easy.

gnuplot and system()

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.