2

C++ プログラムから gnuplot でリアルタイムにグラフをプロットしようとしています.gnuplot 4.6 をインストールしましたが、gnuplot.exe を開いてグラフをプロットできます.しかし、パイプ経由でアプリケーションを開くことができません. これは私が使用したコードです。

#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:program\ は、内部または外部コマンドおよび操作可能なプログラムまたはバッチ ファイルとして認識されません。

同じパスでexeを実行してみましたが、開いていません。cmdプロンプトで指定できる文字列の最大長のためですか..

貴重なご意見をお聞かせください。

ありがとう

4

3 に答える 3

2

バックスラッシュパス区切り文字はエスケープする(またはスラッシュに置き換える)必要があります。

char *path = "C:\\Program Files\\gnuplot\\bin\\wgnuplot";
于 2012-04-09T02:50:19.420 に答える
1

char *パスを使用する代わりに、_popen関数を次のように記述する必要があります。

gp = _popen( "E:\\ myprograms \\ ProgramFiles \\ libraries \\ Gnuplot \\ bin \\ gnuplot -persist"、 "w");

そしておそらくこの方法で対応するデータデータファイルを呼び出す

fprintf(gp、 "splot \" C:\\\\ Users \\\\ username \\\\ Documents \\\\ Visual Studio 2012 \\\\ Projects \\\\ laplace Equation \\\\ laplace.dat \ "\ n");

于 2013-01-20T10:02:53.980 に答える
0

グラフの描画には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;
}

このアプリケーションを Linux でコンパイルして実行するには、

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

これは与える 結果

于 2016-05-11T15:05:15.220 に答える