【发布时间】:2015-01-31 15:54:33
【问题描述】:
我想在模拟过程中做一些实时绘图。为此,我想使用 octave 或 gnuplot。我目前的做法是使用 gnuplot 的前端 feedgnuplot,实际上非常适合。
模拟是用 C++ 编写的,所以我考虑了分叉(feedgnuplot 的新流程)并将相关数据通过管道传输到 feedgnuplot。
我遇到的问题是输出仅在模拟后可见。 但我想看到在模拟过程中更新的情节。
这是一个 MWE:
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
int main()
{
FILE* stream = popen("feedgnuplot", "w");
for(int i = 0; i < 10; ++i)
{
fprintf(stream, "%d\n", i * i);
fflush(stream);
sleep(1);
}
}
程序在 10 秒后停止,显示情节。
当直接在 shell 中使用 feedgnuplot 时,一切都按预期工作。
(即绘制新添加的数据,无需结束进程)
我做错了什么?我想我对 popen 的真正工作原理缺乏一些了解。
【问题讨论】: