首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Gnuplot,来自windows的c++。命令窗口打开和关闭

Gnuplot,来自windows的c++。命令窗口打开和关闭
EN

Stack Overflow用户
提问于 2012-02-19 21:24:29
回答 2查看 14.4K关注 0票数 7

我有以下几点,无论我尝试什么,命令窗口都会再次打开和关闭。不显示绘图,也不写入文件。任何有解决方案的人都可以使用c++的gnuplot。我有4.4和4.6rc1两种版本。

代码语言:javascript
运行
复制
#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);
EN

回答 2

Stack Overflow用户

发布于 2012-12-25 11:10:39

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

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

代码语言:javascript
运行
复制
#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;
}
票数 8
EN

Stack Overflow用户

发布于 2015-01-20 23:16:43

当然,下面的答案与Nicholas Kinar的答案非常相似,唯一增加的一点是如何正确保存.png文件。另外还有几个建议:

  1. 如果输入路径有空格,它将无法工作。在使用Visual Studio2013的Windows8中,它会显示一个错误"C:Program" is not recognized as an internal or external command....
  2. If you not提及任何输出路径,它会打印并保存在C++程序本身的目录中。因此,您必须检查输出路径的正确格式,因为gnuplot没有显示任何错误,因此很难找出确切的原因。

以下是完整的C++程序,它在Windows8.1上的Visual Studio2013上运行良好

代码语言:javascript
运行
复制
#include <cstdio>
#include <iostream>
int main()
{

    FILE* pipe = _popen("C:/gnuplot/bin/pgnuplot.exe", "w");
    if (pipe != NULL)
    {
        fprintf(pipe, "set term win\n");
        fprintf(pipe, "plot(x, sin(x))\n"); //a simple example function
        fprintf(pipe, "set term pngcairo\n");
        fprintf(pipe, "set output \"myFile.png\"\n" );
        fprintf(pipe, "replot\n");
        fprintf(pipe, "set term win\n");
        fflush(pipe);
    }
    else puts("Could not open the file\n");
    _pclose(pipe);
    //system("pause");
    return 0;
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9349538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档