通过管道gnuplot C++接口 - 无法打开wgnuplot

问题描述:

我想从我的C++程序在gnuplot中绘制我的图形实时。我已经安装了gnuplot 4.6,并且能够打开gnuplot.exe和绘图graphs.But但我无法打开应用程序通过管道。这是我用过的代码。通过管道gnuplot C++接口 - 无法打开wgnuplot

#include <stdio.h> 
#include <stdlib.h> 

int main() 
{ 
    FILE* gp; 
    char *path = "C:\Program Files\gnuplot\bin\wgnuplot"; 
#ifdef WIN32 
    gp = _popen("gnuplot -persist", "w"); 
#else 
    gp = _popen(path, "w"); 
#endif 

    if (gp == NULL) 
    return -1; 

    fprintf(gp, "set isosample 100\n"); 
    fprintf(gp, "min=-1\n"); 
    fprintf(gp, "max=1\n"); 
    fprintf(gp, "pi=3.141592\n"); 
    fprintf(gp, "set hidden3d\n"); 
    fprintf(gp, "set pm3d\n"); 
    fprintf(gp, "set contour\n"); 
    fprintf(gp, "splot [min:max] [min:max] x*x+2*y*y-0.3*cos(3*pi*x)-0.4*cos(4*pi*y)+0.7\n"); 
    fprintf(gp, "pause -1\n"); 

    return 0; 
} 

我设置了环境变量,出现以下错误。 c:\程序\不被识别为内部或外部命令和可操作的程序或批处理文件。

我试着用相同的路径运行exe文件,但它没有打开。是否因为在cmd提示符中可以给出的字符串的最大长度..

请给出您的宝贵建议。

感谢

+0

你'#ifdef'构造是奇怪的,考虑到你已经设置为仅在非Windows的情况下才使用非常多的Windows路径。 – zwol 2012-04-09 02:19:05

+0

好吧..我已经下载了一个特定于Windows的库,并使用它.. – ShivShambo 2012-04-09 02:22:57

+0

@Zack是否有任何其他好的库可用于此目的..? – ShivShambo 2012-04-09 02:27:00

反斜线路径分隔符必须进行转义(或斜线代替):

char *path = "C:\\Program Files\\gnuplot\\bin\\wgnuplot"; 
+0

谢谢..现在gnuplot打开,但命令不写入它,没有出现任何情节。 – ShivShambo 2012-04-09 03:39:34

,而不是使用char *的路径,你应该写的_popen功能

GP = _popen(“E:\\ myprograms \\ ProgramFiles \\ libraries \\ Gnuplot \\ bin \\ gnuplot -persist”,“w”);

和可能经由这种方式调用相应数据的数据文件

fprintf中(GP “splot \” C:\\\\用户\\\\用户名\\\\文档\\\\ Visual Studio中2012 \\\ Projects \\\ laplace equation \\\ laplace.dat \“\ n”);

您可以使用gnuplot-cpp来绘制图表。

这个小片段解决您的问题(test1.cpp):

#include "gnuplot_i.hpp" 

int main() 
{ 
    Gnuplot gp; 
    gp.cmd("set isosample 100\n"); 
    gp.cmd("min=-1\n"); 
    gp.cmd("max=1\n"); 
    gp.cmd("pi=3.141592\n"); 
    gp.cmd("set hidden3d\n"); 
    gp.cmd("set pm3d\n"); 
    gp.cmd("set contour\n"); 
    gp.cmd("splot [min:max] [min:max] x*x+2*y*y-0.3*cos(3*pi*x)-0.4*cos(4*pi*y)+0.7\n"); 
    gp.cmd("pause -1\n"); 


    std::cout << std::endl << "Press ENTER to continue..." << std::endl; 
    std::cin.clear(); 
    std::cin.ignore(std::cin.rdbuf()->in_avail()); 
    std::cin.get(); 

    return 0; 
} 

编译和

g++ test1.cpp && ./a.out 

执行在Linux上这个应用程序这给 The result