在线代码实验室
在 /tmp 实测 ofstream 覆盖/追加、ifstream getline/>>、二进制 seekg/tellg/file_size 与 fstream clear+seekp 覆写
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
int main() {
const std::string txt_path = "/tmp/mbsky_ch33_demo.txt";
const std::string num_path = "/tmp/mbsky_ch33_numbers.txt";
const std::string bin_path = "/tmp/mbsky_ch33_pos.dat";
const std::string rw_path = "/tmp/mbsky_ch33_rw.dat";
// 1. ofstream default trunc overwrite, ios::app append, and RAII auto-close
{
std::ofstream out(txt_path);
out << "Old line\n";
}
{
std::ofstream out(txt_path); // truncates previous content
out << "Hello C++\n";
out << "Line two\n";
}
{
std::ofstream app(txt_path, std::ios::app);
app << "Appended line\n";
}
std::vector<std::string> lines;
{
std::ifstream in(txt_path);
std::string line;
while (std::getline(in, line)) {
lines.push_back(line);
}
}
std::ifstream missing("/tmp/mbsky_ch33_nonexistent_file.txt");
bool missing_open = missing.is_open();
std::string act1 = std::to_string(lines.size()) + ":" + lines[0] + "|" + lines[2] +
"|missing=" + (missing_open ? "1" : "0");
const std::string exp1 = "3:Hello C++|Appended line|missing=0";
bool ok1 = (act1 == exp1);
// 2. Formatted extraction (>>) and getline() with spaces
{
std::ofstream out(num_path);
out << "Zara Ali\n100 200\n";
}
std::string read_name;
int a = 0, b = 0;
{
std::ifstream in(num_path);
std::getline(in, read_name);
in >> a >> b;
}
std::string act2 = read_name + ":" + std::to_string(a) + "+" + std::to_string(b);
const std::string exp2 = "Zara Ali:100+200";
bool ok2 = (act2 == exp2);
// 3. Binary seekg / tellg / ios::ate and std::filesystem::file_size
{
std::ofstream bout(bin_path, std::ios::binary);
bout.write("ABCDEFGHIJ", 10);
}
char ch4 = '\0', ch0 = '\0', ch_last = '\0';
std::streampos ate_size = -1;
{
std::ifstream bin(bin_path, std::ios::binary);
bin.seekg(4, std::ios::beg);
bin.get(ch4);
bin.seekg(0, std::ios::beg);
bin.get(ch0);
bin.seekg(-1, std::ios::end);
bin.get(ch_last);
}
{
std::ifstream ate(bin_path, std::ios::binary | std::ios::ate);
ate_size = ate.tellg();
}
auto fs_size = std::filesystem::file_size(bin_path);
std::string act3 = std::string(1, ch4) + std::string(1, ch0) + std::string(1, ch_last) +
"|ate=" + std::to_string(static_cast<long long>(ate_size)) +
"|fs=" + std::to_string(fs_size);
const std::string exp3 = "EAJ|ate=10|fs=10";
bool ok3 = (act3 == exp3);
// 4. Bidirectional fstream: hit EOF, clear() state, seekp() patch, verify
{
std::ofstream init(rw_path, std::ios::binary);
init.write("ABCDEFGHIJ", 10);
}
std::string patched;
{
std::fstream rw(rw_path, std::ios::in | std::ios::out | std::ios::binary);
std::string all;
std::getline(rw, all); // reaches EOF
bool hit_eof = rw.eof();
rw.clear(); // clear eofbit before seeking
rw.seekp(2, std::ios::beg);
rw.put('Z');
rw.flush();
rw.seekg(0, std::ios::beg);
std::getline(rw, patched);
if (!hit_eof) patched = "no_eof";
}
const std::string exp4 = "ABZDEFGHIJ";
bool ok4 = (patched == exp4);
// Clean up temporary files
std::remove(txt_path.c_str());
std::remove(num_path.c_str());
std::remove(bin_path.c_str());
std::remove(rw_path.c_str());
std::cout << "{\n"
<< " \"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\",\n"
<< " \"cases\": [\n"
<< " {\"name\": \"ofstream_trunc_append_and_is_open\", \"expected\": \"" << exp1
<< "\", \"actual\": \"" << act1 << "\", \"passed\": " << (ok1 ? "true" : "false") << "},\n"
<< " {\"name\": \"ifstream_getline_and_formatted_extract\", \"expected\": \"" << exp2
<< "\", \"actual\": \"" << act2 << "\", \"passed\": " << (ok2 ? "true" : "false") << "},\n"
<< " {\"name\": \"binary_seekg_tellg_and_file_size\", \"expected\": \"" << exp3
<< "\", \"actual\": \"" << act3 << "\", \"passed\": " << (ok3 ? "true" : "false") << "},\n"
<< " {\"name\": \"fstream_clear_eof_and_seekp_patch\", \"expected\": \"" << exp4
<< "\", \"actual\": \"" << patched << "\", \"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-files-streams-lab/explore-cpp-files-streams.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-files-streams.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2324.zip