[英]How to exit program execution in C++?
Which function is used in C++ stdlib to exit from program execution with status code?在 C++ stdlib 中使用哪个函数退出带有状态代码的程序执行?
In Java, there's:在 Java 中,有:
System.exit(0)
Assuming you only have one thread:假设你只有一个线程:
#include <iostream>
int main()
{
std::cout << "Hello, World!\n";
return(0);
// PROGRAM ENDS HERE.
std::cout << "You should not see this.\n";
return(0);
}
Output:输出:
Hello, World!
The return(0);
return(0);
can be placed anywhere you like - it'll end int main()
, and hence your program.可以放在任何你喜欢的地方——它会结束int main()
,因此你的程序。
Alternatively, you can call exit(EXIT_SUCCESS);
或者,您可以调用exit(EXIT_SUCCESS);
or exit(EXIT_FAILURE);
或exit(EXIT_FAILURE);
from anywhere you like:从您喜欢的任何地方:
/* exit example */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
FILE * pFile;
pFile = fopen("myfile.txt", "r");
if(pFile == NULL)
{
printf("Error opening file");
exit (1);
}
else
{
/* file operations here */
}
return 0;
}
In addition to the other responses you can also invoke abort, terminate, quick_exit (exits without calling destructors, deallocating etc; hence the name)除了其他响应之外,您还可以调用 abort、terminate、quick_exit(退出时不调用析构函数、解除分配等;因此得名)
terminate calls abort by default but can call any terminate handler you set.终止调用默认中止,但可以调用您设置的任何终止处理程序。
Example usage of abort and set_terminate (to se the handler used by terminate), quick_exit can be called (see example #2) abort 和 set_terminate 的示例用法(设置终止使用的处理程序),可以调用 quick_exit (参见示例 #2)
// set_terminate example
#include <iostream> // std::cerr
#include <exception> // std::set_terminate
#include <cstdlib> // std::abort
void myterminate () {
std::cerr << "terminate handler called\n";
abort(); // forces abnormal termination
}
int main (void) {
std::set_terminate (myterminate);
throw 0; // unhandled exception: calls terminate handler
return 0;
}
quick_exit/at_quick_exit example: quick_exit/at_quick_exit 示例:
/* at_quick_exit example */
#include <stdio.h> /* puts */
#include <stdlib.h> /* at_quick_exit, quick_exit, EXIT_SUCCESS */
void fnQExit (void)
{
puts ("Quick exit function.");
}
int main ()
{
at_quick_exit (fnQExit);
puts ("Main function: Beginning");
quick_exit (EXIT_SUCCESS);
puts ("Main function: End"); // never executed
return 0;
}
I'm not entirely certain why one would call quick_exit but it exists and thus I should provide documentation for it (courtesy of http://www.cplusplus.com/reference )我不完全确定为什么会调用 quick_exit 但它存在,因此我应该为它提供文档(由http://www.cplusplus.com/reference 提供)
Additionally one can call at_exit as the equivalent of at_quick_exit.此外,可以调用 at_exit 作为 at_quick_exit 的等效项。
Admittedly I am not all that familiar with set_terminate and terminate as I don't call them myself, but I would guess you could use quick_exit as a terminate handler if you wanted;诚然,我对 set_terminate 和 terminate 并不十分熟悉,因为我自己不会调用它们,但我想如果您愿意,您可以使用 quick_exit 作为终止处理程序; or a custom one (but please don't quote me on that).或定制的(但请不要引用我的话)。
In C++, you can use exit(0) for example:在 C++ 中,您可以使用 exit(0) 例如:
switch(option){
case 1:
//statement
break;
case 2:
//statement
exit(0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.