从C++中的管道获取 Gnuplot 版本
Get Gnuplot version from pipe in C++
在我的C++程序中(在Linux中(,我可以打开一个管道进行写入并为Gnuplot程序设置值。
FILE *pipe = NULL;
#ifdef WIN32
pipe = _popen("pgnuplot -persist", "w");
#else
pipe = popen("gnuplot", "w");
#endif
if(pipe == NULL)
error("Could not open pipe for write!");
// set title name
fprintf(pipe, "set title 'Sample Points' n");
现在我需要获得 Gnuplot 版本。show version
命令执行此操作,但我如何发送此命令然后读取值。打开管道进行读取似乎对我不起作用,代码卡在 while 循环中而没有获得任何数据。
FILE* pipe = popen(command, "r");
if (!pipe)
{
std::cout << "failed! (can not open pipe)" << endl;
return;
}
char buffer[128];
std::string result = "";
while(!feof(pipe))
{
if(fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
pclose(pipe);
由于在我的 Debian/Linux/Sid/x86-64 上,命令gnuplot --version
输出到stdout
以下行:
gnuplot 5.0 patchlevel 1
我只是推荐
FILE* pipversion = popen("gnuplot --version", "r");
if (!pipversion) { perror("popen gnuplot"); exit(EXIT_FAILURE); };
char lineversion[128];
memset (lineversion, 0, sizeof(lineversion));
if (!fgets(lineversion, sizeof(lineversion), pipversion) {
perror("fgets"); exit(EXIT_FAILURE);
}
/// lineversion is like: gnuplot 5.0 patchlevel 1
int majvers=0, minvers=0, pos= -1;
char* restvers = NULL;
if (sscanf(lineversion, "gnuplot %d.%d %n", &majvers, &minvers, &pos) >= 2) {
assert (pos>=0);
restvers = lineversion+pos;
};
pclose(pipversion);
pipversion = NULL;
之后,majvers
包含gnuplot
的主要版本(例如在我的例子中为 5(,minvers
包含次要版本(例如 0(,restvers
是一个后缀字符串(例如 "patchlevel 1"
没有引号(。
在异常且不太可能的情况下,可能存在潜在的争用条件,即在此popen
和下一个pipe = popen("gnuplot", "w");
之间更新gnuplot
。顺便说一下,命名变量pipe
是糟糕的,因为 POSIX 和 Linux 都有 pipe(2( 系统调用。但我认为不值得关心这种竞争条件。
顺便说一句,你很可能想用 pipe(2( 的显式双重调用替换你的第二个pipe = popen("gnuplot", "w");
(后跟适当的 fork(2( 和 execvp(3( ...(,以便同时gnuplot
输入和输出管道,并在你自己的事件循环中管理它们(可能围绕 poll(2( ...看到这个和那个答案(。
(如果你的应用程序有或使用自己的事件循环,特别是如果它是一个高于Qt或GTK的GUI应用程序,你希望对管道使用相同的事件循环;细节特定于提供该事件循环的库:g_spawn_async_with_pipes & g_source_add_unix_fd for GTK, QQ for Qt ...
我没有时间详细解释如何做到这一点(双重管道进入命令+事件循环(,但高级Linux编程书籍(在线提供(有几章关于这一点。请注意,您需要一些事件循环。
- C++为构建时间获取QDateTime的可靠方法
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 使用指针从C++中的数组中获取最大值
- 如何获取std::result_of函数的返回类型
- 如何在openssl-ecc中获取十六进制格式的私钥
- 使用Unreal C++获取VR耳机的世界位置/方向
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 从C字符串中获取奇怪的字符串长度
- 为什么我的for循环不能正确获取argv
- 从python中调用C++函数并获取返回值
- 如何获取一个数字的前3位
- 获取字符串的长度并将其分配给数组
- 无法获取菜单选择以运行函数.C++
- 数组长度,为什么从命令行获取时不能使用它?
- Boost Spirit,获取迭代器内部语义动作
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 具有默认值的引用获取函数
- xmake总是报告:错误:无法获取cxx的程序,为什么
- 从C++中的管道获取 Gnuplot 版本