# ===== CodeLab: cpp-fstream =====
# 以下代码片段按文章出现顺序拼接, 共 7 段

# ----- 片段 1 (cpp) -----
#include <fstream>
#include <iostream>

int main() {
    std::ofstream out("hello.txt");
    if (!out) {
        std::cerr << "文件打开失败\n";
        return 1;
    }
    out << "Hello, CodeLab!\n";
    out << "第二行:流式输出\n";
    // out 在 main 结束时自动关闭(RAII)
    std::cout << "写入完成\n";
    return 0;
}

# ----- 片段 2 (cpp) -----
#include <fstream>
#include <iostream>
#include <string>

int main() {
    std::ifstream in("hello.txt");
    if (!in) {
        std::cerr << "打开失败\n";
        return 1;
    }
    std::string line;
    int no = 0;
    while (std::getline(in, line)) {
        std::cout << ++no << ": " << line << "\n";
    }
    return 0;
}

# ----- 片段 3 (cpp) -----
#include <fstream>
#include <iostream>

int main() {
    std::ofstream("scores.txt") << "85 92 78\n60 88\n";  // 先造数据
    std::ifstream in("scores.txt");
    int score, sum = 0, count = 0;
    while (in >> score) {
        sum += score;
        ++count;
    }
    std::cout << "共 " << count << " 个分数,总和 " << sum
              << ",平均 " << (count ? static_cast<double>(sum) / count : 0.0)
              << "\n";
    return 0;
}

# ----- 片段 4 (cpp) -----
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

int main() {
    std::ofstream("students.csv") << "姓名,成绩\n"
                                     << "张三,85\n"
                                     << "李四,92\n"
                                     << "王五,78\n";
    std::ifstream in("students.csv");
    std::string line, header;
    std::getline(in, header);          // 跳过表头
    std::vector<std::pair<std::string, int>> rows;
    while (std::getline(in, line)) {
        std::stringstream ss(line);
        std::string name, score_str;
        std::getline(ss, name, ',');   // 按逗号拆分
        std::getline(ss, score_str, ',');
        rows.push_back({name, std::stoi(score_str)});
    }
    int sum = 0;
    for (const auto& r : rows) sum += r.second;
    std::cout << "共 " << rows.size() << " 名学生,平均分 "
              << static_cast<double>(sum) / rows.size() << "\n";
    return 0;
}

# ----- 片段 5 (cpp) -----
#include <fstream>
#include <iostream>

int main() {
    std::ofstream log("log.txt", std::ios::app);  // 追加模式
    log << "2026-08-10 19:00 启动\n";
    log << "2026-08-10 19:01 任务完成\n";
    std::cout << "已追加两条日志\n";

    std::ifstream in("log.txt");
    std::string line;
    while (std::getline(in, line)) std::cout << line << "\n";
    return 0;
}

# ----- 片段 6 (cpp) -----
#include <fstream>
#include <iostream>

int main() {
    // 造一个初始文件:第一行是个计数器
    std::ofstream("counter.txt") << "42\n";
    // 以"读+写"模式打开,不清空文件
    std::fstream fs("counter.txt", std::ios::in | std::ios::out);
    if (!fs) {
        std::cerr << "打开失败\n";
        return 1;
    }
    int value;
    fs >> value;                  // 读出 42,文件指针停在数字后面
    ++value;
    fs.seekp(0);                  // 把写指针拨回文件开头
    fs << value << "\n";          // 覆盖写回
    fs.close();

    std::ifstream in("counter.txt");
    int check;
    in >> check;
    std::cout << "新值: " << check << "\n";   // 输出:43
    return 0;
}

# ----- 片段 7 (cpp) -----
#include <fstream>
#include <iostream>

struct Record {
    int id;
    double score;
};

int main() {
    Record records[] = {{1, 88.5}, {2, 92.0}, {3, 76.5}};
    std::ofstream out("records.bin", std::ios::binary);
    out.write(reinterpret_cast<const char*>(records),
              sizeof(records));
    out.close();

    Record back[3];
    std::ifstream in("records.bin", std::ios::binary);
    in.read(reinterpret_cast<char*>(back), sizeof(back));
    for (const auto& r : back)
        std::cout << r.id << " " << r.score << "\n";
    return 0;
}
