C++Primer 03_IO_文件读写

fstream

fstream是C++标准库中的一个类模板,它提供了对文件进行输入和输出的能力。fstream类是iostream库的一部分,它继承自iostream,并且添加了文件操作特有的功能。以下是一些fstream特有的操作:

  1. 打开和关闭文件

    • open(const char* filename, ios_base::openmode mode):以指定模式打开文件。
    • close():关闭打开的文件。
  2. 设置文件模式

    • ios::app:追加模式,写操作会在文件末尾添加内容。
    • ios::ate:在打开文件后立即定位到文件末尾。
    • ios::binary:二进制模式,用于读写二进制文件。
    • ios::in:输入模式。
    • ios::out:输出模式。
    • ios::trunc:截断模式,如果文件存在,截断文件内容。
  3. 检查文件状态

    • is_open():检查文件是否成功打开。
    • eof():检查是否到达文件末尾。
  4. 文件定位

    • seekg(pos_type pos):将输入指针移动到指定位置。
    • seekp(pos_type pos):将输出指针移动到指定位置。
    • tellg():返回当前输入位置。
    • tellp():返回当前输出位置。
  5. 文件大小

    • seekg(0, ios::end)tellg() 可以用于获取文件的大小。
  6. 读写操作

    • 除了iostream提供的读写操作外,fstream还支持直接读写二进制数据。
  7. 错误处理

    • fail():检查是否有错误发生。
    • clear():清除错误状态。
  8. iostream的兼容性

    • 由于fstream继承自iostream,所以可以使用iostream中的所有输入输出操作符和函数。

下面是一个使用fstream打开文件并写入数据的简单示例:

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

int main() {
    std::fstream inFileStream, outFileStream;
    std::string line;

    // 打开输入文件
    inFileStream.open("input.txt", std::ios::in);
    if (!inFileStream.is_open()) {
        std::cerr << "Unable to open input file!" << std::endl;
        return 1;
    }

    // 打开输出文件
    outFileStream.open("output.txt", std::ios::out);
    if (!outFileStream.is_open()) {
        std::cerr << "Unable to open output file!" << std::endl;
        inFileStream.close();
        return 1;
    }

    // 读取输入文件并写入到输出文件
    while (getline(inFileStream, line)) {
        outFileStream << line << std::endl;
    }

    // 关闭文件
    inFileStream.close();
    outFileStream.close();

    return 0;
}

在C++中,iostreamfstreamstringstream 是处理输入输出流的三个不同的组件,它们都定义在 <ios><iostream><fstream><sstream> 头文件中。下面是对这三个组件的详细解析:

iostream

iostream 是C++标准库中最基本的输入输出流库,提供了输入输出流的基类。iostream 包含两个主要的流对象:

  • std::cin:标准输入流,通常用于从键盘读取数据。
  • std::cout:标准输出流,通常用于向屏幕输出数据。
  • std::cerr:标准错误流,用于输出错误信息。

iostream 还定义了 std::istreamstd::ostream 类,它们是 std::cinstd::cout 的基类。

fstream

fstream 是文件输入输出流库,提供了对文件进行读写操作的能力。fstream 包含两个主要的类模板:

  • std::ifstream:用于从文件读取数据。
  • std::ofstream:用于向文件写入数据。

fstream 类继承自 iostream 类,因此它们拥有 iostream 的所有功能,并且添加了文件操作特有的功能,如打开、关闭文件等。

stringstream

stringstream 是字符串输入输出流库,允许你使用与 iostreamfstream 类似的操作来处理字符串数据。stringstream 包含两个主要的类模板:

  • std::istringstream:用于从字符串读取数据。
  • std::ostringstream:用于向字符串写入数据。

stringstream 类型的对象可以像使用 std::cinstd::cout 一样使用,但是它们操作的是字符串而不是文件或标准输入输出。这使得它们非常适合于处理内存中的字符串数据,例如解析字符串或构建字符串输出。

示例代码

使用 iostream:

#include <iostream>

int main() {
    int num;
    std::cout << "Enter a number: ";
    std::cin >> num;
    std::cout << "You entered: " << num << std::endl;
    return 0;
}

使用 fstream:

#include <fstream>

int main() {
    std::ofstream outfile("example.txt");
    if (outfile.is_open()) {
        outfile << "Writing to a file!";
        outfile.close();
    } else {
        std::cerr << "Unable to open file!" << std::endl;
    }
    return 0;
}

使用 stringstream:

#include <sstream>

int main() {
    std::string data = "The value is 42";
    std::istringstream iss(data);
    int value;
    iss >> value;
    std::cout << "The value is: " << value << std::endl;
    return 0;
}

在实际编程中,根据需要处理的数据源(标准输入输出、文件、内存中的字符串)选择合适的流库。

  • 15
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值