蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

实测 <errno.h> 的 ENOENT 文件错误捕获、EDOM/ERANGE 数学错误与 errno 复位机制

C本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。explore-standard-library-errno-h.c
explore-standard-library-errno-h.c53 行
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>

int main(void) {
    errno = 0;
    FILE *fp = fopen("/tmp/mbsky_definitely_missing_errno_file.txt", "r");
    int open_err = errno;
    const char *msg = strerror(open_err);
    int has_msg = (msg != NULL && strlen(msg) > 0) ? 1 : 0;
    if (fp != NULL) {
        fclose(fp);
    }
    bool c1_pass = (fp == NULL && open_err == ENOENT && has_msg == 1);

    errno = 0;
    volatile double neg = -1.0;
    volatile double dom_res = sqrt(neg);
    int dom_err = errno;
    int dom_is_nan = isnan(dom_res) ? 1 : 0;

    errno = 0;
    volatile double huge_exp = 1000.0;
    volatile double rng_res = exp(huge_exp);
    int rng_err = errno;
    int rng_is_inf = isinf(rng_res) ? 1 : 0;
    bool c2_pass = (dom_err == EDOM && dom_is_nan == 1 && rng_err == ERANGE && rng_is_inf == 1);

    errno = EINVAL;
    int set_val = (errno == EINVAL) ? 1 : 0;
    errno = 0;
    int cleared = (errno == 0) ? 1 : 0;
    int distinct = (EDOM != ERANGE && ENOENT != EINVAL && EPERM != EACCES) ? 1 : 0;
    bool c3_pass = (set_val == 1 && cleared == 1 && distinct == 1);

    printf(
        "{"
        "\"runtime\":\"GCC C11 (mstorsjo/llvm-mingw:20260616)\","
        "\"cases\":["
        "{\"name\":\"fopen_nonexistent_enoent\",\"expected\":\"null=1,enoent=1,msg=1\",\"actual\":\"null=%d,enoent=%d,msg=%d\",\"passed\":%s},"
        "{\"name\":\"math_domain_and_range_errno\",\"expected\":\"edom=1,nan=1,erange=1,inf=1\",\"actual\":\"edom=%d,nan=%d,erange=%d,inf=%d\",\"passed\":%s},"
        "{\"name\":\"errno_lvalue_reset_and_macros\",\"expected\":\"set=1,clr=1,distinct=1\",\"actual\":\"set=%d,clr=%d,distinct=%d\",\"passed\":%s}"
        "]"
        "}\n",
        (fp == NULL) ? 1 : 0, (open_err == ENOENT) ? 1 : 0, has_msg, c1_pass ? "true" : "false",
        (dom_err == EDOM) ? 1 : 0, dom_is_nan, (rng_err == ERANGE) ? 1 : 0, rng_is_inf, c2_pass ? "true" : "false",
        set_val, cleared, distinct, c3_pass ? "true" : "false"
    );
    return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。

入口文件:projects/c-standard-library-errno-h-lab/explore-standard-library-errno-h.c

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-standard-library-errno-h.c,或下载右侧课程工程包。
  2. 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:C

课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip

下载课程关联工程包:C

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3243.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习