VisualC++ から gnuplot を操作する方法
Windows上で、VisualC++ から「gnuplot」を操作します(gnuplot のインストールはこちら)。 C言語のパイプという機能を利用して、gnuplot にコマンドを直接送り込んでグラフをプロットします。
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
FILE *fp = _popen("C:/gnuplot/bin/pgnuplot.exe", "w");
if (fp == NULL) return -1;
fputs("plot sin(x)\n", fp);
fflush(fp);
cin.get(); //<------------------------------------- 一時停止のために入れる
_pclose(fp);
return 0;
}
「pgnuplot.exe」はパイプ用の実行ファイルです(wgnuplot.exeではありません)。 「#include <iostream>」は「cin.get()」の使用のために利用します。 「cin.get()」を入れないと、「_pclose(fp)」の実行でgnuplotの出力画面が消えてしまいます。 例えば、アニメーションを作成時に、連続的でgifファイルを生成する際には、「cin.get()」は必要ないですね。



