如何解决popen后如何继续运行c ++代码?
我正在研究数值分析主题,并尝试使用c ++编写代码。
我想做的是, 首先,我想调用gnuplot程序并绘制函数。但是我是通过使用popen函数做到的。
然后,我看到绘制的图,并输入一个值以同时计算问题的解决方案。(我失败了。)
问题是打开gnuplot程序后程序无法继续运行。
使用popen后如何继续运行c ++代码?
这是我编写的代码
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <assert.h>
#include <cstring>
using namespace std;
//const char func1[30] = "(10*x)*sin(x*100)";
void plotFunction(char function[50],double x_min,double x_max);
char func1[30] = "sin(3*x)";
char func2[50] = "sinh(x)";
int main()
{
plotFunction(func1,0.0,6.3);
plotFunction(func2,-3,3);
cout << "process finished" << endl;
return 0;
}
void plotFunction(char function[50],double x_max)
{
char range[100];
char min[10]; char max[10];
sprintf(min,"%f",x_min);
sprintf(max,x_max);
strcpy(range,"set xrange [");
strcat(range,min);
strcat(range,":");
strcat(range,max);
strcat(range,"]\n");
char plot[200];
strcpy(plot,"plot ");
strcat(plot,function);
strcat(plot,"\n");
FILE* plotHandle = NULL;
if (plotHandle == NULL) {
plotHandle = _popen("C:\\Program\" \"Files\\gnuplot\\bin\\gnuplot.exe -continue","w");
assert(plotHandle != NULL);
}
fprintf(plotHandle,"set terminal wxt\n");
fprintf(plotHandle,"set xlabel \"X\"\n");
fprintf(plotHandle,"set ylabel \"Y\"\n");
fprintf(plotHandle,range);
fprintf(plotHandle,"set grid\n");
fprintf(plotHandle,"plot (10*x)*sin(x*100)\n");
fprintf(plotHandle,plot);
//fflush(plotHandle);
//_pclose(plotHandle);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。