我想在程序终止之前用 ctrl-c 中断时向文件中写入几行。然而,文件的位置不是硬编码的,所以我需要比正常中断处理更多的东西正如这里所解释的 https://stackoverflow.com/questions/1641182/how-can-i-catch-a-ctrl-c-event-c。做这个的最好方式是什么?
动机:
我正在做与时间相关的有限元模拟。有时我忘记或错误估计了合理的 tmax,并且模拟需要很长时间才能完成,所以我用 ctrl-c 中断它。但是我仍然想使用到目前为止生成的数据。特别是有一个 xml 文件,在模拟完成后需要附加几行来关闭标签。当然,可以手动完成此操作,但这很痛苦,所以我尝试将其自动化。
基于“的答案”如何捕获 ctrl-c 事件? (C++) https://stackoverflow.com/questions/1641182/how-can-i-catch-a-ctrl-c-event-c“我们可以安装一个仅引发异常的信号处理程序。然后我们可以捕获异常并运行我们喜欢的任何标准 C++ 代码。
这是一个工作示例:
#include <iostream>
#include <exception>
#include <signal.h>
#include <stdlib.h>
class InterruptException : public std::exception
{
public:
InterruptException(int s) : S(s) {}
int S;
};
void sig_to_exception(int s)
{
throw InterruptException(s);
}
int main()
{
// Taken from answer to "How can I catch a ctrl-c event? (C++)"
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = sig_to_exception;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
try
{
std::cout << "Inside try" << std::endl;
// Do something with output so the loop doesn't get optimised out
double i = 0;
while(i < 1e30) {i++;} // loop until interrupted
std::cout << i << std::endl;
}
catch(InterruptException& e)
{
std::cout << "Write something to file" << std::endl;
std::cout << "Caught signal " << e.S << std::endl;
return 1;
}
return 0;
}
这种方法至少有一个缺点:在尝试安装处理程序后,我们必须包装所有内容,否则中断信号将导致 abort()。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)