第11.12章: C++ iostream

English

概述

C++ 的标准 I/O 库提供 std::fstream、std::ifstream 和 std::ofstream。这些流类建立在 std::basic_filebuf 之上,那是 C++ 运行时的带缓冲文件抽象。

fast_io 提供包装,让你能把 std::basic_filebuf 接入 fast_io 的 I/O 系统,从而对它们使用 print、println 和 scan。

标准 C++ I/O:<iostream>、<fstream>、<sstream>

在讨论 fast_io 如何包装 C++ I/O 之前,先回顾一下标准 C++ I/O 库。C++ 为 I/O 提供三个主要头文件:

标准流

C++ 提供四个预定义流:


#include <iostream>
#include <string>

int main() {
    int x;
    std::string name;

    // Read from stdin
    std::cout << "Enter your name: ";
    std::cin >> name;

    std::cout << "Enter a number: ";
    std::cin >> x;

    // Write to stdout
    std::cout << "Hello, " << name << "!\n";
    std::cout << "You entered: " << x << "\n";

    // Write to stderr
    if (x < 0) {
        std::cerr << "Warning: negative number\n";
    }
}

文件流


#include <fstream>
#include <iostream>
#include <string>

int main() {
    // Write to a file
    std::ofstream fout("output.txt");
    if (!fout) {
        std::cerr << "Failed to open file\n";
        return 1;
    }

    fout << "Hello, World!\n";
    fout << "The answer is: " << 42 << "\n";
    fout.close();

    // Read from a file
    std::ifstream fin("output.txt");
    if (!fin) {
        std::cerr << "Failed to open file\n";
        return 1;
    }

    std::string line;
    while (std::getline(fin, line)) {
        std::cout << "Read: " << line << "\n";
    }
    fin.close();

    // Read and write (bidirectional)
    std::fstream fio("data.txt", std::ios::in | std::ios::out | std::ios::trunc);
    fio << "Initial data\n";
    fio.seekg(0);  // Go back to beginning
    std::getline(fio, line);
    std::cout << "Read back: " << line << "\n";
}

常见的文件打开模式:

字符串流


#include <sstream>
#include <iostream>
#include <string>

int main() {
    // Write to a string
    std::ostringstream oss;
    oss << "x = " << 42 << ", pi = " << 3.14159;
    std::string result = oss.str();
    std::cout << "Built string: " << result << "\n";

    // Read from a string
    std::istringstream iss("42 3.14 hello");
    int x;
    double y;
    std::string word;
    iss >> x >> y >> word;
    std::cout << "Parsed: " << x << ", " << y << ", " << word << "\n";

    // Bidirectional string stream
    std::stringstream ss;
    ss << "100 200";
    int a, b;
    ss >> a >> b;
    std::cout << "Sum: " << (a + b) << "\n";
}

流的操纵器


#include <iostream>
#include <iomanip>

int main() {
    // Formatting
    std::cout << std::hex << 255 << "\n";           // ff
    std::cout << std::oct << 255 << "\n";           // 377
    std::cout << std::dec << 255 << "\n";           // 255

    // Floating-point precision
    std::cout << std::fixed << std::setprecision(2) << 3.14159 << "\n";  // 3.14
    std::cout << std::scientific << 3.14159 << "\n";  // 3.14e+00

    // Width and alignment
    std::cout << "[" << std::setw(10) << std::left << "left" << "]\n";
    std::cout << "[" << std::setw(10) << std::right << "right" << "]\n";

    // Boolean
    std::cout << std::boolalpha << true << "\n";    // true
    std::cout << std::noboolalpha << true << "\n";  // 1
}

附录

完整的 C++ iostream 与 iomanip 参考见:

要点