0

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:

enter image description here

user137
  • 282
  • 2
  • 14
  • 1
    In your relative path test do you actually mean to have it be under the root directory rather than current? Add a leading . to the path if it's supposed to be relative to the current directory. – SoronelHaetir Dec 28 '17 at 07:12
  • @SoronelHaetir I made that change, but it didn't solve the problem. But you probably saved me from future problems. Thank you. – user137 Dec 28 '17 at 08:07

1 Answers1

0

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.

Bob__
  • 12,361
  • 3
  • 28
  • 42
user137
  • 282
  • 2
  • 14
  • 2
    It's the C++ preprocessor that converts the escaped sequence `"\\"` into a single char `'\'`. Have you tried using `'/'` instead, for the path? – Bob__ Jan 03 '18 at 09:16
  • @Bob__ That appears to work too. I should have figured that out faster, because I had originally written this program under linux, and it worked fine. When I moved it to a windows computer, I changed the slashes "because windows". I guess the real take away is that C++ preprocessor plus whatever gnuplot does makes two rounds of escape character conversion, so you need to plan for that and add extra slashes. – user137 Jan 04 '18 at 01:16