Gnuplot,来自Windows的C ++.命令窗口打开和关闭 [英] Gnuplot, c++ from windows. command window opens and closes

查看:148
本文介绍了Gnuplot,来自Windows的C ++.命令窗口打开和关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容,无论我尝试什么,都将再次打开和关闭命令窗口.没有显示图,没有写入文件.有解决方案的人可以使用c ++中的gnuplot.我有4.4和4.6rc1可用.

I have the following, and no matter what i try a command window is opened and closed again. No plots are shown, no files are written. Anyone who have a solution to use gnuplot from c++. I have both 4.4 and 4.6rc1 available.

#ifdef WIN32
  gp = _popen("C:\Program Files (x86)\gnuplot\bin\pgnuplot.exe", "w");
#else
  gp = popen("gnuplot -persist", "w");
#endif


 if (gp == NULL)
       return -1;

  /* fprintf(gp, "unset border\n");
  fprintf(gp, "set clip\n");
  fprintf(gp, "set polar\n");
  fprintf(gp, "set xtics axis nomirror\n");
  fprintf(gp, "set ytics axis nomirror\n");
  fprintf(gp, "unset rtics\n");
  fprintf(gp, "set samples 160\n");
  fprintf(gp, "set zeroaxis");
    fprintf(gp, "  set trange [0:2*pi]");*/


  fprintf(gp, "set term png\n");
  fprintf(gp, "set output \"c:\\printme.png\"");
  fprintf(gp, "plot .5,1,1.5\n");
   fprintf(gp, "pause -1\n");

     fflush(gp);

推荐答案

以下程序已经在Windows上使用Visual Studio和MinGW编译器进行了测试,以及在GNU/Linux上使用gcc进行了测试. gnuplot二进制文件必须在路径上,并且在Windows上,必须使用管道的pgnuplot版本的二进制文件.

The following program has been tested on Windows using the Visual Studio and MinGW compilers, as well as on GNU/Linux using gcc. The gnuplot binary must be on the path, and on Windows, the piped pgnuplot version of the binary must be used.

我发现Windows管道比GNU/Linux上的管道慢得多.对于大型数据集,在Windows上通过管道将数据传输到gnuplot的速度很慢,而且通常不可靠.此外,按键等待代码在GNU/Linux上更有用,一旦调用pclose(),绘图窗口将关闭.

I've found that Windows pipes are much slower than the corresponding ones on GNU/Linux. For large datasets, transferring data to gnuplot over a pipe on Windows is slow and often unreliable. Moreover, the key press waiting code is more useful on GNU/Linux, where the plot window will close once pclose() has been called.

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

// Tested on:
// 1. Visual Studio 2012 on Windows
// 2. Mingw gcc 4.7.1 on Windows
// 3. gcc 4.6.3 on GNU/Linux

// Note that gnuplot binary must be on the path
// and on Windows we need to use the piped version of gnuplot
#ifdef WIN32
    #define GNUPLOT_NAME "pgnuplot -persist"
#else 
    #define GNUPLOT_NAME "gnuplot"
#endif

int main() 
{
    #ifdef WIN32
        FILE *pipe = _popen(GNUPLOT_NAME, "w");
    #else
        FILE *pipe = popen(GNUPLOT_NAME, "w");
    #endif

    if (pipe != NULL)
    {
        fprintf(pipe, "set term wx\n");         // set the terminal
        fprintf(pipe, "plot '-' with lines\n"); // plot type
        for(int i = 0; i < 10; i++)             // loop over the data [0,...,9]
            fprintf(pipe, "%d\n", i);           // data terminated with \n
        fprintf(pipe, "%s\n", "e");             // termination character
        fflush(pipe);                           // flush the pipe

        // wait for key press
        std::cin.clear();
        std::cin.ignore(std::cin.rdbuf()->in_avail());
        std::cin.get();

        #ifdef WIN32
                _pclose(pipe);
        #else
                pclose(pipe);
        #endif
    }
    else
        std::cout << "Could not open pipe" << std::endl; 
return 0;
}

这篇关于Gnuplot,来自Windows的C ++.命令窗口打开和关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆