【C++】文件输入输出流

C++ 中的文件流用于处理文件的读写操作。主要包括文件输入流、文件输出流和文件输入输出流。以下是对这些文件流的详细介绍:

1. 文件输出流 (std::ofstream)

std::ofstream 是文件输出流,用于将数据写入文件。它是 std::ostream 的一个派生类。

基本用法
#include <fstream>
#include <iostream>

int main() {
    std::ofstream outfile("example.txt"); // 创建并打开文件
    if (outfile.is_open()) {
        outfile << "Writing this to a file." << std::endl;
        outfile.close(); // 关闭文件
    } else {
        std::cerr << "Unable to open file" << std::endl;
    }
    return 0;
}

2. 文件输入流 (std::ifstream)

std::ifstream 是文件输入流,用于从文件读取数据。它是 std::istream 的一个派生类。

基本用法
#include <fstream>
#include <iostream>
#include <string>

int main() {
    std::ifstream infile("example.txt"); // 打开文件
    if (infile.is_open()) {
        std::string line;
        while (getline(infile, line)) { // 按行读取文件
            std::cout << line << std::endl;
        }
        infile.close(); // 关闭文件
    } else {
        std::cerr << "Unable to open file" << std::endl;
    }
    return 0;
}

3. 文件输入输出流 (std::fstream)

std::fstream 是文件输入输出流,支持同时进行读写操作。它是 std::iostream 的一个派生类。

基本用法
#include <fstream>
#include <iostream>

int main() {
    std::fstream file("example.txt", std::ios::in | std::ios::out | std::ios::app); // 打开文件,支持读写和追加
    if (file.is_open()) {
        file << "Appending this to the file." << std::endl; // 写入数据
        file.seekg(0); // 将文件指针移到文件开头
        std::string line;
        while (getline(file, line)) { // 读取文件内容
            std::cout << line << std::endl;
        }
        file.close(); // 关闭文件
    } else {
        std::cerr << "Unable to open file" << std::endl;
    }
    return 0;
}

文件打开模式

在使用文件流时,可以指定文件的打开模式。常见的打开模式如下:

  • std::ios::in:读模式。
  • std::ios::out:写模式。
  • std::ios::binary:二进制模式。
  • std::ios::ate:初始位置在文件末尾。
  • std::ios::app:追加模式,每次写入数据都追加到文件末尾。
  • std::ios::trunc:截断模式,如果文件存在,则先清空文件。
示例:使用不同的打开模式
#include <fstream>
#include <iostream>

int main() {
    // 以写模式打开文件,并截断文件内容
    std::ofstream outfile("example.txt", std::ios::out | std::ios::trunc);
    if (outfile.is_open()) {
        outfile << "This will overwrite the file." << std::endl;
        outfile.close();
    } else {
        std::cerr << "Unable to open file" << std::endl;
    }

    // 以读模式打开文件
    std::ifstream infile("example.txt", std::ios::in);
    if (infile.is_open()) {
        std::string line;
        while (getline(infile, line)) {
            std::cout << line << std::endl;
        }
        infile.close();
    } else {
        std::cerr << "Unable to open file" << std::endl;
    }

    return 0;
}

二进制文件操作

对于二进制文件,可以使用 std::ios::binary 打开模式进行操作。

示例:写入和读取二进制文件
#include <fstream>
#include <iostream>

int main() {
    // 写入二进制文件
    std::ofstream outfile("example.bin", std::ios::binary);
    if (outfile.is_open()) {
        int data = 12345;
        outfile.write(reinterpret_cast<char*>(&data), sizeof(data));
        outfile.close();
    } else {
        std::cerr << "Unable to open file" << std::endl;
    }

    // 读取二进制文件
    std::ifstream infile("example.bin", std::ios::binary);
    if (infile.is_open()) {
        int data;
        infile.read(reinterpret_cast<char*>(&data), sizeof(data));
        std::cout << "Data read from file: " << data << std::endl;
        infile.close();
    } else {
        std::cerr << "Unable to open file" << std::endl;
    }

    return 0;
}

处理文件流错误

可以通过 fail()bad()eof() 等成员函数检查文件流的状态。

示例:检查文件流状态
#include <fstream>
#include <iostream>

int main() {
    std::ifstream infile("nonexistent.txt");

    if (!infile) {
        std::cerr << "Error opening file" << std::endl;
    }

    if (infile.eof()) {
        std::cerr << "End of file reached" << std::endl;
    }

    if (infile.fail()) {
        std::cerr << "Logical error on I/O operation" << std::endl;
    }

    if (infile.bad()) {
        std::cerr << "Read/write error on I/O operation" << std::endl;
    }

    return 0;
}

总结

  • 文件输出流 (std::ofstream):用于将数据写入文件。
  • 文件输入流 (std::ifstream):用于从文件读取数据。
  • 文件输入输出流 (std::fstream):支持同时进行读写操作。
  • 文件打开模式:可以指定不同的打开模式,如读模式、写模式、二进制模式等。
  • 二进制文件操作:使用 std::ios::binary 打开模式进行二进制文件的读写操作。
  • 文件流状态检查:可以通过成员函数检查文件流的状态,以处理错误情况。

通过合理使用这些文件流和打开模式,可以有效地处理各种文件读写操作。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值