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

验证流插入/提取链式等价、<iomanip> 格式化及 cerr/clog 缓冲标志差异

CPP本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。explore-cpp-basic-input-output.cpp
explore-cpp-basic-input-output.cpp88 行
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

static std::string json_escape(const std::string& s) {
    std::string out;
    for (unsigned char c : s) {
        if (c == '\"') out += "\\\"";
        else if (c == '\\') out += "\\\\";
        else if (c == '\n') out += "\\n";
        else out.push_back(static_cast<char>(c));
    }
    return out;
}

int main() {
    // 1. Stream insertion chaining (same ostream interface as cout)
    std::ostringstream oss;
    char str[] = "Hello C++";
    int version = 17;
    oss << "Value of str is : " << str << " | C++" << version;
    std::string act1 = oss.str();
    const std::string exp1 = "Value of str is : Hello C++ | C++17";
    bool ok1 = (act1 == exp1);

    // 2. Chained extraction vs sequential extraction (same istream interface as cin)
    std::istringstream iss1("BlueSky 2026");
    std::string name1;
    int year1 = 0;
    iss1 >> name1 >> year1;

    std::istringstream iss2("BlueSky 2026");
    std::string name2;
    int year2 = 0;
    iss2 >> name2;
    iss2 >> year2;

    std::string act2 = name1 + ":" + std::to_string(year1) + "=" + name2 + ":" + std::to_string(year2);
    const std::string exp2 = "BlueSky:2026=BlueSky:2026";
    bool ok2 = (act2 == exp2);

    // 3. <iomanip> parameterized manipulators
    std::ostringstream fmt;
    fmt << std::setw(6) << std::setfill('0') << 42 << "|" << std::fixed << std::setprecision(2) << 3.14159;
    std::string act3 = fmt.str();
    const std::string exp3 = "000042|3.14";
    bool ok3 = (act3 == exp3);

    // 4. cerr (unitbuf) vs clog (buffered) and cin.tie() == &cout
    bool cerr_unitbuf = (std::cerr.flags() & std::ios::unitbuf) != 0;
    bool clog_unitbuf = (std::clog.flags() & std::ios::unitbuf) != 0;
    bool cin_tied_cout = (std::cin.tie() == &std::cout);

    std::ostringstream err_cap, log_cap;
    std::streambuf* old_cerr = std::cerr.rdbuf(err_cap.rdbuf());
    std::streambuf* old_clog = std::clog.rdbuf(log_cap.rdbuf());
    std::cerr << "Error message : Unable to read....";
    std::clog << "Log message : Ready" << std::flush;
    std::cerr.rdbuf(old_cerr);
    std::clog.rdbuf(old_clog);

    std::string act4 =
        std::string("cerr_unitbuf=") + (cerr_unitbuf ? "1" : "0") +
        ",clog_unitbuf=" + (clog_unitbuf ? "1" : "0") +
        ",cin_tied=" + (cin_tied_cout ? "1" : "0") +
        ",err=" + err_cap.str() +
        ",log=" + log_cap.str();
    const std::string exp4 =
        "cerr_unitbuf=1,clog_unitbuf=0,cin_tied=1,err=Error message : Unable to read....,log=Log message : Ready";
    bool ok4 = (act4 == exp4);

    std::cout << "{\n"
              << "  \"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\",\n"
              << "  \"cases\": [\n"
              << "    {\"name\": \"stream_insertion_chaining\", \"expected\": \"" << json_escape(exp1)
              << "\", \"actual\": \"" << json_escape(act1) << "\", \"passed\": " << (ok1 ? "true" : "false") << "},\n"
              << "    {\"name\": \"stream_extraction_chaining\", \"expected\": \"" << json_escape(exp2)
              << "\", \"actual\": \"" << json_escape(act2) << "\", \"passed\": " << (ok2 ? "true" : "false") << "},\n"
              << "    {\"name\": \"iomanip_formatting\", \"expected\": \"" << json_escape(exp3)
              << "\", \"actual\": \"" << json_escape(act3) << "\", \"passed\": " << (ok3 ? "true" : "false") << "},\n"
              << "    {\"name\": \"cerr_clog_buffering_and_tie\", \"expected\": \"" << json_escape(exp4)
              << "\", \"actual\": \"" << json_escape(act4) << "\", \"passed\": " << (ok4 ? "true" : "false") << "}\n"
              << "  ]\n"
              << "}\n";
    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-basic-input-output-lab/explore-cpp-basic-input-output.cpp

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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