gnuplot and system()

B

Blah

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?
 
R

Robert Bauck Hamar

Blah said:
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::eek:fstream 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.
 
E

Ethan Merritt

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.
..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top