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

实测 <stdio.h> 文本文件读写删除、tmpfile 二进制随机定位与 snprintf/sscanf/ungetc

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

int main(void) {
    const char *path = "/tmp/mbsky_stdio_c11_test.txt";
    FILE *wf = fopen(path, "w");
    int wrote_ok = 0;
    if (wf != NULL) {
        fprintf(wf, "Hello, World!\nLine=%d\n", 2);
        fflush(wf);
        fclose(wf);
        wrote_ok = 1;
    }

    char line1[64] = {0};
    char line2[64] = {0};
    int eof_seen = 0;
    FILE *rf = fopen(path, "r");
    if (rf != NULL) {
        if (fgets(line1, sizeof(line1), rf) == NULL) line1[0] = '\0';
        if (fgets(line2, sizeof(line2), rf) == NULL) line2[0] = '\0';
        char extra[16];
        if (fgets(extra, sizeof(extra), rf) == NULL && feof(rf)) {
            eof_seen = 1;
        }
        fclose(rf);
    }
    int rm_rc = remove(path);
    bool c1_pass = (
        wrote_ok == 1 &&
        strcmp(line1, "Hello, World!\n") == 0 &&
        strcmp(line2, "Line=2\n") == 0 &&
        eof_seen == 1 &&
        rm_rc == 0
    );

    FILE *tf = tmpfile();
    int nums[4] = {10, 20, 30, 40};
    size_t nw = 0, nr = 0;
    long pos_zero = -1;
    int third_val = 0;
    int fpos_val = 0;
    int no_err = 0;
    if (tf != NULL) {
        nw = fwrite(nums, sizeof(int), 4, tf);
        rewind(tf);
        pos_zero = ftell(tf);
        fseek(tf, (long)(2 * sizeof(int)), SEEK_SET);
        fpos_t saved;
        fgetpos(tf, &saved);
        nr = fread(&third_val, sizeof(int), 1, tf);
        fsetpos(tf, &saved);
        if (fread(&fpos_val, sizeof(int), 1, tf) != 1) fpos_val = -1;
        clearerr(tf);
        no_err = (ferror(tf) == 0) ? 1 : 0;
        fclose(tf);
    }
    bool c2_pass = (nw == 4 && nr == 1 && pos_zero == 0 && third_val == 30 && fpos_val == 30 && no_err == 1);

    char fmt_buf[32];
    int sn_len = snprintf(fmt_buf, sizeof(fmt_buf), "id=%d,tag=%s", 42, "c11");
    int parsed_id = 0;
    char parsed_tag[16] = {0};
    int sc_cnt = sscanf(fmt_buf, "id=%d,tag=%15s", &parsed_id, parsed_tag);

    int unget_ch = 0;
    FILE *uf = tmpfile();
    if (uf != NULL) {
        fputs("AB", uf);
        rewind(uf);
        int ch = fgetc(uf);
        ungetc(ch, uf);
        unget_ch = fgetc(uf);
        fclose(uf);
    }
    int macros_ok = (EOF < 0 && BUFSIZ > 0 && FILENAME_MAX > 0 && FOPEN_MAX >= 8) ? 1 : 0;
    bool c3_pass = (sn_len == 13 && sc_cnt == 2 && parsed_id == 42 && strcmp(parsed_tag, "c11") == 0 && unget_ch == 'A' && macros_ok == 1);

    printf(
        "{"
        "\"runtime\":\"GCC C11 (mstorsjo/llvm-mingw:20260616)\","
        "\"cases\":["
        "{\"name\":\"file_write_read_fgets_fprintf\",\"expected\":\"wrote=1,eof=1,rm=0\",\"actual\":\"wrote=%d,eof=%d,rm=%d\",\"passed\":%s},"
        "{\"name\":\"binary_tmpfile_fseek_fpos\",\"expected\":\"nw=4,zero=0,v3=30,fpos=30,noerr=1\",\"actual\":\"nw=%zu,zero=%ld,v3=%d,fpos=%d,noerr=%d\",\"passed\":%s},"
        "{\"name\":\"snprintf_sscanf_ungetc_macros\",\"expected\":\"sc=2,id=42,ch=65,macros=1\",\"actual\":\"sc=%d,id=%d,ch=%d,macros=%d\",\"passed\":%s}"
        "]"
        "}\n",
        wrote_ok, eof_seen, rm_rc, c1_pass ? "true" : "false",
        nw, pos_zero, third_val, fpos_val, no_err, c2_pass ? "true" : "false",
        sc_cnt, parsed_id, unget_ch, macros_ok, 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-stdio-h-lab/explore-standard-library-stdio-h.c

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

下载课程关联工程包:C

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

下载课程关联工程包:C

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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