在调用GNUPLOT之后,如何继续执行C 代码
How to continue to execute the C++ code after gnuplot was called?
我正在使用GCC作为编译器,而gnuplot-iostream.h作为流以组合C 代码和GNUPLOT功能的流。
我在做什么:
我尝试通过gnuplot使数据拟合,并从生成的fit.log文件中提取最终拟合参数以进行进一步处理。
问题是什么:
执行像这样的代码
std::cout << "Starting to fit" << std::endl;
if (bStartFit == true)
{
// gp << doing stuf here;
std::cout << "Test end" << std::endl;
}
std::cout << "Fit is done" << std::endl;
输出将是:
Starting to fit
Fit is done
Test end
//gnuplot stuff
我的问题是:如何强制代码在需要时准确地执行GNUPLOT的内容,以及在使用C 代码之后。例如:
- 写入介绍;
- 绘制sin(x)函数(快速示例);
- 等到gnuplot关闭;
- 写出gnuplot后写出退出消息或做任何事情。
谢谢,p
编辑:
std::string filename = "fit.log";
if (bStartFit == true)
{
// Using Gnuplot for the data fit (command are parsed as the strings):
// 1. define the fit function.
gp << "f(x) = imfpZP * x**(-b) + c * x**(d) + g * x**(h) n";
// 2. fit parameters first assumption.
gp << "b = 1.1; c = 0.5; d = 1.0; g = 2.0; h = 0.1 n";
// 3. fit range.
gp << "fit [50:10000] f(x) 'agn.iimfp' via b,c,d,g,h n";
// 4. set the logarithmic scale.
gp << "set logscale n";
// 5. plot the fitted data.
gp << "plot 'agn.iimfp' w l lw 2 tit 'orig', f(x) w l lw 2 tit 'fit' n";
std::cout << "Fit was successful" << std::endl;
}
// Opening the generated fit.log file to store the fit parameters:
std::ifstream inFIT(filename.c_str());
if (inFIT.is_open())
{
std::cout << "FIT log is opened" << std::endl;
std::string line;
int lineCounter = 0;
while (std::getline(inFIT, line))
{
lineCounter++;
}
std::cout << "Total lines: " << lineCounter << std::endl;
// Getting the five lines with fit parameters from the fit.log:
std::fstream& GoToLine(std::fstream& file, unsigned int lineNumber);
std::fstream file(filename.c_str());
GoToLine(file, lineCounter - 15);
std::string b_Line;
std::getline(file, b_Line);
std::cout << b_Line << std::endl;
std::istringstream sb(b_Line);
std::string tempStr;
char tempChar;
sb >> tempStr >> tempChar >> b
// similar code to get another 4 lines
;
它是特定于操作系统的。我猜您在Linux上(或至少在某些POSIX OS上)。然后,您确实应该阅读高级Linux编程。并且使用strace(1)可能有助于了解正在发生的事情。
您可以使用Popen(3),但是您可能应该明确使用系统调用(它们在Syscalls(2)中列出),例如Pipe(2),Fork(2),DUP2(2),Execve(2),waitpid(2)等。很可能有一些事件循环(例如,围绕民意调查(2))。
顺便说一句,您应该知道输入输出已被缓冲,并且您可能要确保您的 gnuplot stream 定期刷新(因此,请使用std::endl
或std::flush
适当地使用在上面)。编写n
是不够的!您可能至少应该编码
gp << "plot 'agn.iimfp' w l lw 2 tit 'orig', f(x) w l lw 2 tit 'fit' "
<< std::endl;
(我用std::endl
的明确使用替换了字符串中的一些n
)
我对 gnuplot stream (我猜这有点像一些专业的popen
,但使用C 流),但是我想您应该GNUPLOT print
或printerr
命令要与您的通话程序交流,绘制了给定曲线的事实。(但是然后您需要一个真正的事件循环,并且您正在定义gnuplot
和您的程序之间的双向协议。)。
也许QT或POCO可能是相关的,因为它们提供了事件循环和过程的一些概念。也许您可能有几个线程(一个线程(一个管理gnuplot
,另一个用于其余的),但随后您有同步问题。
(我不够理解您的程序应该做什么以及您如何编码,所以这是一个盲目的猜测;我觉得您的问题非常不清楚)
阅读有关过程间通信的更多信息。
- C++ 在信号处理程序后继续执行
- 超时后,线程睡眠不会继续执行
- 在构造函数中删除后继续执行
- c++:if 语句无法正常运行,并继续执行无效输出
- 英特尔 Pin:RECEIVED_ACCESS_FAULT异常后正确继续执行
- 如果发生错误,则返回错误值,否则,继续执行函数
- 睡眠线程或进程的指令是否在线程/进程休眠时继续执行?
- 使用 UWP 中的 IAsyncOperation 继续执行任务
- 是什么导致程序在我继续执行另一个功能之前停止
- 使用 ASSERT_DEATH 时,有没有办法在应用程序终止后自动继续执行所有测试
- 现代C++.从继续执行的工作线程返回数据结构
- 在击中断点并继续执行VS时,如何在运行时自动修改变量的值
- 在调用GNUPLOT之后,如何继续执行C 代码
- OpenMP 构造,在至少 1 个线程完成后立即继续执行
- 如果我已经完成了当前功能,我该如何选择继续执行另一个功能
- 循环浏览文件夹中的所有文件以继续执行脚本
- 在程序继续执行的同时,如何使代码的两部分之间存在时间间隔或延迟?C++
- C++if-else语句继续执行if,而不是else下的代码
- 继续执行另一个线程
- pthread_join()是否允许在调用线程上继续执行