C ++ Windows gnuplot_i.hpp不会保存到其他文件夹

时间:2017-12-28 06:38:29

标签: c++ gnuplot gnuplot-iostream

我一直在使用gnuplot_i.hpp来绘制来自C ++程序的大量数据。我需要能够将输出文件(png图像)放入其他文件夹中。但是当我尝试将输出设置为其他文件夹时,无论是相对地址还是绝对地址,它都会失败。 Gnuplot给了我一个“无法打开文件,输出没有改变错误”。我可以把文件放到当前目录中。

我不知道这是不是很愚蠢,比如文件名问题,或者不太直观,比如权限问题。但我确实注意到我的程序在gnuplot完成之前“完成”。我不知道这是否与错误有关,但也许我想让gnuplot在给它任务B之前完成任务A.

以下是一个示例代码:

#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;
}

这是输出:

enter image description here

1 个答案:

答案 0 :(得分:0)

我偶然玩弄了gnuplot应用程序本身,偶然发现了解决方案。 gnuplot_i.hpp创建一个gnuplot进程,并将所有文本发送到进程以进行绘图。但是gnuplot需要将\ _字符转义为"\\"。我以为我在上面的代码中这样做了,但是gnuplot_i.hpp必须剥离转义字符,所以gnuplot只收到1。这导致gnuplot将文件名解释为坏转义字符代码。在将我的代码更改为使用4 \字符后,它似乎可以工作,因为2 \被传递给gnuplot并且文件名被正确解释。