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

实测 <cstdio> 文本/格式化/二进制/单字符文件读写、fseek/ftell 与 EOF/feof/ferror

CPP本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。explore-cpp-libs-cstdio.cpp
explore-cpp-libs-cstdio.cpp160 行
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

namespace lab {

struct Case {
    std::string name;
    std::string expected_json;
    std::string actual_json;
    bool passed;
};

std::string esc(const std::string& s) {
    std::string out = "\"";
    for (unsigned char c : s) {
        switch (c) {
            case '\"': out += "\\\""; break;
            case '\\': out += "\\\\"; break;
            case '\n': out += "\\n"; break;
            case '\r': out += "\\r"; break;
            case '\t': out += "\\t"; break;
            default:
                if (c < 0x20) {
                    char buf[8];
                    std::snprintf(buf, sizeof(buf), "\\u%04x", c);
                    out += buf;
                } else {
                    out += static_cast<char>(c);
                }
        }
    }
    out += "\"";
    return out;
}

std::string jbool(bool v) { return v ? "true" : "false"; }

void emit(const std::vector<Case>& cases) {
    std::cout << "{\"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [";
    for (std::size_t i = 0; i < cases.size(); ++i) {
        if (i) std::cout << ", ";
        std::cout << "{\"name\": " << esc(cases[i].name)
                  << ", \"expected\": " << cases[i].expected_json
                  << ", \"actual\": " << cases[i].actual_json
                  << ", \"passed\": " << jbool(cases[i].passed) << "}";
    }
    std::cout << "]}\n";
}

} // namespace lab

int main() {
    std::vector<lab::Case> cases;

    // Case 1: fopen / fputs / fflush / fgets / fclose + NULL check on missing file
    const char* text_path = "/tmp/mbsky_cstdio_text.txt";
    std::FILE* f = std::fopen(text_path, "w");
    bool write_ok = false;
    if (f != NULL) {
        std::fputs("Hello, World!\n", f);
        write_ok = (std::fflush(f) == 0);
        std::fclose(f);
    }
    char line[64] = {};
    f = std::fopen(text_path, "r");
    bool read_ok = false;
    if (f != NULL) {
        read_ok = (std::fgets(line, sizeof(line), f) != NULL);
        std::fclose(f);
    }
    std::FILE* missing = std::fopen("/tmp/mbsky_definitely_missing_file_98765.txt", "r");
    bool missing_is_null = (missing == NULL);
    std::remove(text_path);
    bool c1 = write_ok && read_ok && (std::string(line) == "Hello, World!\n") && missing_is_null;
    cases.push_back({"text_file_open_write_read_cleanup", lab::jbool(true), lab::jbool(c1), c1});

    // Case 2: fprintf / fscanf + snprintf / sscanf
    const char* fmt_path = "/tmp/mbsky_cstdio_fmt.txt";
    f = std::fopen(fmt_path, "w");
    if (f) {
        std::fprintf(f, "%d %.5f\n", 42, 3.14159);
        std::fclose(f);
    }
    int parsed_num = 0;
    float parsed_pi = 0.0f;
    f = std::fopen(fmt_path, "r");
    int scanned = 0;
    if (f) {
        scanned = std::fscanf(f, "%d %f", &parsed_num, &parsed_pi);
        std::fclose(f);
    }
    std::remove(fmt_path);

    char membuf[64] = {};
    std::snprintf(membuf, sizeof(membuf), "Value: %d", 42);
    int read_val = 0;
    int s_scanned = std::sscanf(membuf, "Value: %d", &read_val);
    bool c2 = (scanned == 2) && (parsed_num == 42) && (std::fabs(parsed_pi - 3.14159f) < 1e-5f) &&
              (s_scanned == 1) && (read_val == 42) && (std::string(membuf) == "Value: 42");
    cases.push_back({"formatted_fprintf_fscanf_and_snprintf_sscanf", lab::jbool(true), lab::jbool(c2), c2});

    // Case 3: Binary fwrite / fread / fseek / ftell
    const char* bin_path = "/tmp/mbsky_cstdio_bin.bin";
    int out_arr[4] = {10, 20, 30, 12345};
    f = std::fopen(bin_path, "wb");
    std::size_t nw = 0;
    if (f) {
        nw = std::fwrite(out_arr, sizeof(int), 4, f);
        std::fclose(f);
    }
    long byte_size = -1;
    int in_arr[4] = {};
    std::size_t nr = 0;
    f = std::fopen(bin_path, "rb");
    if (f) {
        std::fseek(f, 0, SEEK_END);
        byte_size = std::ftell(f);
        std::fseek(f, 0, SEEK_SET);
        nr = std::fread(in_arr, sizeof(int), 4, f);
        std::fclose(f);
    }
    std::remove(bin_path);
    bool c3 = (nw == 4) && (nr == 4) && (byte_size == static_cast<long>(4 * sizeof(int))) &&
              (std::memcmp(out_arr, in_arr, sizeof(out_arr)) == 0);
    cases.push_back({"binary_fwrite_fread_fseek_ftell", lab::jbool(true), lab::jbool(c3), c3});

    // Case 4: Character-level fputc / fgetc / EOF / feof / ferror
    const char* ch_path = "/tmp/mbsky_cstdio_ch.txt";
    f = std::fopen(ch_path, "w");
    if (f) {
        std::fputc('A', f);
        std::fputc('B', f);
        std::fputc('C', f);
        std::fclose(f);
    }
    std::string collected;
    int eof_flag = 0;
    int err_flag = 1;
    f = std::fopen(ch_path, "r");
    if (f) {
        int ch;
        while ((ch = std::fgetc(f)) != EOF) {
            collected.push_back(static_cast<char>(ch));
        }
        eof_flag = std::feof(f);
        err_flag = std::ferror(f);
        std::fclose(f);
    }
    std::remove(ch_path);
    bool c4 = (collected == "ABC") && (eof_flag != 0) && (err_flag == 0);
    cases.push_back({"char_io_fputc_fgetc_eof_ferror", lab::jbool(true), lab::jbool(c4), c4});

    lab::emit(cases);
    return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。

入口文件:projects/cplusplus-cpp-libs-cstdio-lab/explore-cpp-libs-cstdio.cpp

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

下载课程关联工程包:C++

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

下载课程关联工程包:C++

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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