C++同时控制另一个程序的 I/O
C++ control I/O of another program simultaneously
我正在程序中控制 Gnuplot 以进行拟合和绘图;但是,为了恢复拟合参数,我想使用 Gnuplot 的打印函数:
FILE *pipe = popen("gnuplot -persist", "w");
fprintf(pipe, "v(x) = va_1*x+vb_1n");
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 ++和通常的侏儒终端。
我找到了这个现成的答案:popen() 可以制作像 pipe() + fork() 这样的双向管道吗?
在你提供的pfunc
中,你必须dup2
作为参数收到的文件描述符,以stdin
stdout
,然后exec
gnuplot,例如:
#include <unistd.h>
void gnuplotProcess (int rfd, int wfd)
{
dup2( STDIN_FILENO, rfd );
dup2( STDOUT_FILENO, wfd );
execl( "gnuplot", "gnuplot", "-persist" );
}
int fds[2];
pid_t gnuplotPid = pcreate(fds, gnuplotProcess);
// now, talk with gnuplot via the fds
我省略了任何错误检查。
相关文章:
- 如何使用C++读取另一个程序中的源代码输出
- 如何将一个窗口保留在另一个应用程序窗口的前面
- 将库链接到另一个应用程序时,CMAKE 找不到库
- 程序在使用 system() 启动另一个可执行文件时停止
- 使用另一个程序调用正在运行的 C++ 程序中的函数
- 调试控制台 ChessEngine.exe "used"在另一个应用程序(国际象棋 GUI)中
- C++将结构复制到另一个程序的资源
- 如何在 qt 中从另一个线程运行 qt并发时关闭程序
- 试图让C++程序循环回到代码中的另一个点
- 为什么在我的程序中输入另一个输入会给我不同的结果
- 在 Windows 10 中将一个应用程序放在另一个应用程序前面
- 将数据从一个应用程序单向发送到另一个应用程序的最佳做法
- C++启动另一个程序(客户端-服务器)的程序
- 如何在 eclipse 中将标准输入从控制台更改为另一个程序
- 当电子的窗口隐藏时如何激活另一个应用程序窗口
- 与 Python 中子进程中的另一个应用程序的交互式会话
- 如何在另一个终端中打开并稍后关闭来自c ++的另一个程序?
- 将UWP应用程序的简单INT发送到另一个程序
- 如何将整个C Glut程序编写为可以在另一个程序中调用的函数
- osX - 如何从我的 QT 应用程序启动另一个程序