PDA

View Full Version : Using system() with gnuplot in qt



Splatify
25th February 2011, 12:00
Hi all I am trying to use system() with gnu plot.

I have some code which looks like this:



system("gnuplot");
system("set term jpeg");
//...more code here
system("exit");



However all it does it run the first system call and then stops with the command prompt window open. You close this command prompt window and it then calls the next system call, which of course errors because it is not aware of the former command. I was wondering how i get around this problem. Any help would be much appreciated.

I've tried using qwt before you say anything, and lets just say it caused me a lot of problem, doesn't offer the functionality i require and seems far harder to use. Therefore I'm sticking with gnuplot.

I'm using windows 7, Home Premium 32 bit.

Thanks for your time and trouble.

high_flyer
25th February 2011, 12:49
Read about using QProcess.

SixDegrees
25th February 2011, 12:56
Every system call launches a new shubshell and runs the provided command in that shell. If you want to use system() in this way, you'll have to combine your shell commands into one, something like:


string cmd = "gnuplot; set term jpeg; ... ;exit";
system(cmd);

wysota
25th February 2011, 16:29
You can also use popen() if you want to stick to plain C. I think that's closer to what you want to achieve.

Splatify
28th February 2011, 18:29
How would I go about using this popen() function?

Thanks

ChrisW67
28th February 2011, 22:20
I can see three options:
Write your gnuplot commands to a temporary file and execute:
system("gnuplot <tempfilename"); You can use mkstemp (3) or QTemporaryFile to get a safe temporary file.
Open gnuplot using QProcess and write your gnuplot commands to the sub-process input stream that QProcess provides.
popen (3) gnuplot and use the FILE handle returned to write your gnuplot commands before pclose (3)