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

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

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

基本用法
#include<fstream>#include<iostream>intmain(){
    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;}return0;}

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

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

基本用法
#include<fstream>#include<iostream>#include<string>intmain(){
    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;}return0;}

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

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

基本用法
#include<fstream>#include<iostream>intmain(){
    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;}return0;}

文件打开模式

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

  • std::ios::in :读模式。
  • std::ios::out :写模式。
  • std::ios::binary :二进制模式。
  • std::ios::ate :初始位置在文件末尾。
  • std::ios::app :追加模式,每次写入数据都追加到文件末尾。
  • std::ios::trunc :截断模式,如果文件存在,则先清空文件。
示例:使用不同的打开模式
#include<fstream>#include<iostream>intmain(){// 以写模式打开文件,并截断文件内容
    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;}return0;}

二进制文件操作

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

示例:写入和读取二进制文件
#include<fstream>#include<iostream>intmain(){// 写入二进制文件
    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;}return0;}

处理文件流错误

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

示例:检查文件流状态
#include<fstream>#include<iostream>intmain(){
    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;}return0;}

总结

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

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