退出带有状态的 C++ 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6006329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exit c++ program with status
提问by smitrp
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)
回答by Mateen Ulhaq
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);
can be placed anywhere you like - it'll end int main()
, and hence your program.
该return(0);
可以像任何你放置-这将结束int main()
,因此你的程序。
Alternatively, you can call exit(EXIT_SUCCESS);
or exit(EXIT_FAILURE);
from anywhere you like:
或者,您可以拨打exit(EXIT_SUCCESS);
或exit(EXIT_FAILURE);
从您喜欢的任何地方拨打电话:
/* 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;
}
回答by smitrp
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; or a custom one (but please don't quote me on that).
诚然,我对 set_terminate 和 terminate 并不十分熟悉,因为我自己不会调用它们,但我想如果需要,您可以使用 quick_exit 作为终止处理程序;或定制的(但请不要引用我的话)。
回答by Leonardo Amadori Lima
In C++, you can use exit(0) for example:
在 C++ 中,您可以使用 exit(0) 例如:
switch(option){
case 1:
//statement
break;
case 2:
//statement
exit(0);