C++同时控制另一个程序的I/O
我在程序中控制 Gnuplot 进行拟合和绘图;但是,为了恢复拟合参数,我想使用 Gnuplot 的打印功能:
FILE *pipe = popen("gnuplot -persist", "w");
fprintf(pipe, "v(x) = va_1*x+vb_1\n");
fprintf(pipe, "fit v(x) './file' u 1:2 via va_1,vb_1 \n")
fprintf(pipe, "print va_1"); // outputs only the variable's value as a string to
// a new line in terminal, this is what I want to get
...
pclose(pipe);
我已经阅读了很多有关 popen()
、fork()
等的内容,但是这里或其他网站上提供的答案要么缺乏彻底的解释,与我的问题无关,要么太难理解(我刚刚开始编程)。
仅供参考:我使用的是 Linux、g++ 和常用的 gnome 终端。
I'm controlling Gnuplot within my program for fitting and plotting; however, to get the fit parameters back, I want to use Gnuplot's print-function:
FILE *pipe = popen("gnuplot -persist", "w");
fprintf(pipe, "v(x) = va_1*x+vb_1\n");
fprintf(pipe, "fit v(x) './file' u 1:2 via va_1,vb_1 \n")
fprintf(pipe, "print va_1"); // outputs only the variable's value as a string to
// a new line in terminal, this is what I want to get
...
pclose(pipe);
I've read a lot about popen()
, fork()
and so on but the answers presented here or on other sites were either lacking a thorough explanation, not related to my problem or simply too hard to understand (I'm just beginning to program).
FYI: I'm using Linux, g++ and the usual gnome-terminal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了这个现成的答案: Can popen () 制作像 pipe() + fork() 这样的双向管道?
在您提供的
pfunc
中,您必须dup2
作为<的参数接收的文件描述符code>stdin 一个stdout
然后exec
gnuplot,例如:我省略了任何错误检查。
I found this ready-to-use answer: Can popen() make bidirectional pipes like pipe() + fork()?
In the
pfunc
you supply, you have todup2
the file descriptors received as arguments tostdin
anstdout
and thenexec
gnuplot, for example:I omitted any error checking.