I have been using gnuplot_i.hpp to plot lots of data from a C++ program. I need to be able to put the output files (png images) into other folders. But when I try to set the output to other folders, either with relative or absolute addresses, it fails. Gnuplot gives me a "cannot open file, output not changed error". I can put a file into the current directory.
I don't know if this is something dumb, like a file name issue, or something less intuitive, like a permissions issue. But I did notice that my program "finishes" before gnuplot finishes. I don't know if that has something to do with the errors, but maybe I'd like to let gnuplot finish task A before I give it task B.
Here is an example code:
#include <iostream>
#include <string>
#include "../gnuplot_i.hpp"
int main(){
Gnuplot g("null");
std::cout << "Test Start" << std::endl;
// Put file in current directory...
std::string current_dir_string = "set terminal pngcairo enhanced\r\n"
"set output \"test1.png\"\r\n"
"$DATA << EOD\r\n"
"1 1\r\n"
"2 2\r\n"
"3 3\r\n"
"EOD\r\n"
"plot $DATA with linespoints\r\n";
// Put file in test directory with absolute address...
std::string test_dir_string1 = "set terminal pngcairo enhanced\r\n"
"set output \"C:\\Users\\REDACTED\\Documents\\PROGRAMMING\\Test\\testdir\\test2.png\"\r\n"
"$DATA << EOD\r\n"
"1 1\r\n"
"2 2\r\n"
"3 3\r\n"
"EOD\r\n"
"plot $DATA with linespoints\r\n";
// Put file in test directory with relative address...
std::string test_dir_string2 = "set terminal pngcairo enhanced\r\n"
"set output \".\\testdir\\test3.png\"\r\n" // ADDED THE . ACCORDING TO SoronelHaetir'S COMMENT, BUT DIDN'T UPDATE OUTPUT IMAGE.
"$DATA << EOD\r\n"
"1 1\r\n"
"2 2\r\n"
"3 3\r\n"
"EOD\r\n"
"plot $DATA with linespoints\r\n";
// Put file in test directory with number in name...
g.cmd(current_dir_string); // THIS WORKS
g.cmd(test_dir_string1); // THIS FAILS
g.cmd(test_dir_string2); // THIS FAILS
std::cout <<std::endl;
std::cout << "Test Finished" << std::endl;
return 0;
}
And here is the output:
I stumbled across the solution by playing around with the gnuplot application itself for a while. gnuplot_i.hpp creates a gnuplot process, and sends all text to the process for plotting. But gnuplot needs the \ characters to be escaped as
"\\"
. I thought I was doing that in my code above, but gnuplot_i.hpp must have been stripping off the escape characters, so that gnuplot received only 1 . This caused gnuplot to interpret the file names as bad escape character codes. After changing my code to use 4 \ characters, it seems to work, because 2 \ are passed to gnuplot and the file name gets correctly interpreted.