-1

I wrote blew code in C++:

#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;
}

this program opens a file and writes the blew python code in it:

name = input("Enter your name: ")
print("Hello", name)

then runs it.

when program runs this code:

system("script_temp_file_id_b01.py");

the script will be executed.

But if user close the program during script executing the two following lines will not execute and the script file will not delete:

system("attrib script_temp_file_id_b01.py -h -s -a");
system("del script_temp_file_id_b01.py");

How can I do this?

Monster22
  • 1
  • 2
  • It a program terminates, then it is terminated. How can you imagine that something can be done, or clarify what you want. Disabling the ability to stop the program? Or? – Jean-Baptiste Yunès Nov 26 '15 at 20:47

1 Answers1

0

It depends how run your program. If you interrupt your program execution with CTRL-C you can catch this event. In this case this answer will help you.

Generally it's not possible to run code from the program after the execution. But you can catch most events that could cause your program to get killed in an early stage.

Community
  • 1
  • 1
Rolf Lussi
  • 615
  • 5
  • 16