C++-如何在程序终止后完成一些任务
C++ - How to do some tasks after program is terminated
我在C++中编写了blow代码:
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
ofstream sf;
sf.open("script_temp_file_id_b01.py", ios::out);
system("attrib script_temp_file_id_b01.py +h +s +a");
sf << "name = input("Enter your name: ")n";
sf << "print("Hello", name)";
sf.close();
system("script_temp_file_id_b01.py");
system("attrib script_temp_file_id_b01.py -h -s -a");
system("del script_temp_file_id_b01.py");
return 0;
}
该程序打开一个文件并在其中写入blow python代码:
name = input("Enter your name: ")
print("Hello", name)
然后运行它。
当程序运行此代码时:
system("script_temp_file_id_b01.py");
脚本将被执行。
但是,如果用户在脚本执行期间关闭程序,则以下两行将不会执行,脚本文件也不会删除:
system("attrib script_temp_file_id_b01.py -h -s -a");
system("del script_temp_file_id_b01.py");
我该怎么做?
这取决于程序的运行方式。如果您使用CTRL-C中断程序执行,则可以捕获此事件。在这种情况下,这个答案会对你有所帮助。
通常情况下,在执行之后不可能从程序中运行代码。但你可以捕捉到大多数可能导致你的程序在早期阶段被杀的事件。
相关文章:
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 我收到以下错误:抛出'std::bad_alloc'实例后终止调用
- 为什么这个 c++ 代码打印出长度 5,当我打印出字符串时,程序会自动终止?
- 有没有任务栏API可以立即应用注册表更改
- 当我在其中一个线程执行中(在activemq-cpp中)捕获到特定值时,我如何终止/停止所有其他线程
- llvm构建器向基本块添加终止符
- 如何使用隔离>终止执行来停止所有线程
- 用于将C++代码转换为 Web 程序集的脚本未终止
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- 终止 QProcess 不会终止子进程
- 如何创建线程序列以按照启动顺序执行任务?
- C++一个线程如何正确通信其任务已完成?
- 在线程之间拆分任务总是值得的吗?
- 运行代码时,c++ 会终止进程
- 使应用程序/服务,以便在任务管理器中尝试结束/终止其进程将导致"Unable to Terminate Process"
- 终止Qt进程:Windows任务管理器在做什么,我没有?
- C++-如何在程序终止后完成一些任务
- 如何在TBB中终止和停止任务
- 如何在应用程序通过任务管理器终止时保存数据