HOME > natural science Laboratory > コンピュータ・シミュレーション講座 > TIPS 集

VisualC++ から gnuplot を操作する方法

文責:遠藤 理平 (2009年12月31日) カテゴリ:TIPS 集(107)備忘録(18)

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()」は必要ないですね。

20091231-1.gif

参考ページ

gnuplot をC/C++から使う方法(工藤俊亮氏)



▲このページのトップNPO法人 natural science トップ

関連記事

TIPS 集

備忘録

▲このページのトップNPO法人 natural science トップ




Warning: mysqli_connect(): (28000/1045): Access denied for user 'xsvx1015071_ri'@'sv102.xserver.jp' (using password: YES) in /home/xsvx1015071/include/natural-science/include_counter-d.php on line 8
MySQL DBとの接続に失敗しました