もちっとメモ

もちっとメモ

もぐりのエンジニアが日々の中で試してみたことを気が向いたときに書き連ねていきます

Cで出力したファイルをリアルタイムでGnuplotに飛ばす

    
        #define _USE_MATH_DEFINES
        #include <math.h>
        #include <fstream>
        #include <iostream>
        using namespace std;
        void test(int tmp);
        int t;
        int t_max;
        double dt;
        int main(){
         int judge = 2;
         do{
          test(judge);
          FILE *hoge;
          hoge = _popen("gnuplot -persist", "w");
          fprintf_s(hoge, \
                    "plot 'test.dat' with lines lw 2 notitle \n");
          fflush(hoge);
          fprintf_s(hoge, "exit");
          _pclose(hoge);
          cout << "Do you finish ? 1:yes, 2:change, 3:continue" << endl;
          scanf("%d", &judge);
          if (judge == 1) break;
         } while (1);
         return 0;
        }
        
        void test(int tmp){
         if (tmp == 2){
          cout << "パラメータを指定してください" << endl;
          cout << "t_max=";
          scanf_s("%d", &t_max);
          cout << "dt=";
          scanf_s("%lf", &dt);
         }
         char filepath[100];
         sprintf(filepath, "test.dat");
         ofstream output(filepath, ios_base::trunc);
         for (t = 0; t < (int)(t_max / dt); t++){
          output << t << " " << sin(t) << endl;
         }
        }