C++ stdlib 中使用哪个函数退出程序执行并返回状态码?
在 Java 中,有:
System.exit(0)
最佳答案
假设你只有一个线程:
#include <iostream>
int main()
{
std::cout << "Hello, World!\n";
return(0);
// PROGRAM ENDS HERE.
std::cout << "You should not see this.\n";
return(0);
}
输出:
Hello, World!
return(0);
可以放在任何你喜欢的地方 - 它会结束 int main()
,然后你的程序。
或者,您可以调用 exit(EXIT_SUCCESS);
or 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;
}
关于c++ - 如何在 C++ 中退出程序执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6006329/