c++ - 如何在 C++ 中退出程序执行?

标签 c++ exit

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/

相关文章:

c++ - 如何在 C++ 中对文件系统进行非并发打印?

c++ - 在 C++ 中,为什么 <cctype> 定义了 std::isspace 和::isspace?

c# - 申请.退出

c++ - glibc 检测到错误

c++ - NULL 的非指针等价物是什么?

c++ - 重载函数产生不明确的错误

python - 如何删除Python IDLE中的exit(0)弹出窗口?

windows - 关闭时批量运行脚本

c - 确定要使用的正确循环

javascript - selenium-webdriver:如何在 javascript 中使用 driver.quit()