Gnuplot, c++ from windows. command window opens and closes

前端 未结 2 685
Happy的楠姐
Happy的楠姐 2020-12-30 12:12

I have the following, and no matter what i try a command window is opened and closed again. No plots are shown, no files are written. Anyone who have a solution to use gnupl

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-30 12:48

    Of course, following answer is quite similar to the answer of Nicholas Kinar, the only added point is how to save the .png file properly. Also a couple of suggestions:

    1. If input path has spaces, it won't work. In windows 8 using Visual Studio 2013, it shows an error "C:Program" is not recognized as an internal or external command....
    2. If you do not mention any output path, it prints and saves in the directory of C++ program itself. So you must check the proper format for output path, as gnuplot doesn't shows any error, it is difficult to spot the exact reason.

    Following is the complete C++ program which works just fine on Visual Studio 2013 on Windows 8.1

    #include 
    #include 
    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;
    }
    

提交回复
热议问题