I am looking for a command that will restart/reload the program. Any help is appreciated.
Also, code to end the program would be nice as well.
Printable View
I am looking for a command that will restart/reload the program. Any help is appreciated.
Also, code to end the program would be nice as well.
Just to be sure: do you really want to restart the program or do you simply want to do whatever the program is doing one more time? If the latter, use a looping structure to start execution all over.
To end program, use return in main and exit(n) (I think) in other functions.
You can probably use system(MyProg.exe) to start the program.
If you wanted the program to re-start itself... Well, the program can't do anything after it ends and exits... You could have the program start a 2nd instance of itself, and then end the 1st instance. (Assuming that you are running Windows or another multitasking operating system that would allow you to run two instances of the same program at the same time... And, assuming there's nothing about the program that prevents two instances of it running.)
anon is right. In most cases, it's better to use a loop. Many programs are written with almost everything inside one big main loop. Inside that loop, you might find more "nested" loops, and several if-statements.
If you are new to programming, I suggest working through all of the tutorials before trying anything new.
Right, exit() from <cstdlib>. Pass it an int, which is the return value, just like from main(). Usually you'd use exit(0) (or exit(EXIT_SUCCESS), or maybe exit(1) (or exit(EXIT_FAILURE)) on error.Quote:
To end program, use return in main and exit(n) (I think) in other functions.
As mentioned, from main, you can just use a return statement.
In posix you can use
To set the exit handler which is called on process termination. maybe you can pass it your main function to allow your app to resurrect itself all the time. But main has the signature int(*)(void), so it would be a bad hack if it works at all. Better use on step of indirection: write a functionCode:
#include <stdlib.h>
int atexit(void (*function)(void));
I've never tried this...Code:void handler()
{
main();
}
Calling main() or taking its address is forbidden by the C++ standard, even if no compiler I know enforces this.
atexit() is ANSI standard as well as POSIX.
Besides, you'd end up with code like this:
So why not call loop() directly? It would be the same thing.Code:#include <stdlib.h>
void loop(void) {
atexit(loop);
if(condition) exit(0);
}
int main() {
loop();
return 0;
}
Also, it would be hard to guarantee that the code would not call exit() again, and both ANSI and POSIX specify that if exit() is called from within a function registered by atexit(), the result is undefined. So with that code, you'd only be able to restart the program once.
Not to mention that it's a recursive solution so that you'd probably* eventually run out of stack space eventually.
* Some compilers can optimise recursive code into iterative code.
In short, that is a very bad idea.
Ok, I probably eventually got it eventually :) thx