• C++高级教程学习---文件和流


    前言

    学习如何从文件读取流和向文件写入流。


    文件和流

    fstream标准库 中定义了三个新的数据类型:

    • ofstream:该数据类型表示输出文件流,用于创建文件并向文件写入信息。
    • ifstream: 该数据类型表示输入文件流,用于从文件读取信息。
    • fstream:该数据类型通常表示文件流,且同时具有ofstram和ifstream俩种功能,这意味着它可以创建文件,向文件写入信息,从文件读取信息。

    打开文件

    从文件读取信息或者向文件写入信息之前,必须先打开文件。

    ofstream和fstream对象都可以用来打开文件进行读写操作,如果只需要打开文件进行读操作,则使用ifstream对象。

    下面是open()函数的标准语法,open()函数是fstream、ifstream和ofstream对象的一个成员。

    void open(const cha *filename, ios::openmode mode);
    
    • 1

    在这里,open()成员函数的第一参数指定要打开的文件的名称和位置,第二个参数定义文件被打开的模式。

    模式选择:

    • ios::app 追加模式。所有写入都追加到文件末尾。
    • ios::ate 文件打开后定位到文件末尾。
    • ios::in 打开文件用于读取。
    • ios::out 打开文件用于写入。
    • ios::trunc 如果该文件已经存在,其内容将在打开文件之前被截断,即把文件长度设为0。

    可以将上述俩种或俩种以上的模式结合使用,例如你想要以写入模式打开文件,并希望截断文件,以防止文件已存在,那么可以使用下面的语法:

    ofstream outfile;
    outfile.open("file.dat", ios::out | ios::trunc);
    
    • 1
    • 2

    类似的如果想要打开一个文件用于读写,可以使用:

    ifstream afile;
    afile.open("file.dat", ios::out | ios::in);
    
    • 1
    • 2

    关闭文件

    当C++程序终止时,它会自动刷新所有流,释放所有分配的内存,并关闭所有打开的文件。但程序员应该养成一个好习惯,在程序终止前关闭所有打开的文件。

    下面是close()函数的标准语法,close()函数也是fstream、ifstream和ofstream对象的一个成员。

    void close();
    
    • 1

    写入文件

    在C++编程中,我们使用**流插入运算符(<<)**向文件写入信息。

    outputFile << "This is some data that I'm writing to the file." << std::endl;
    
    • 1

    读取文件

    在C++编程中,我们使用**流提取运算符(>>)**从文件读取信息。

       // 从输入文件中读取整数并计算他们的和
        while (inputFile >> number){
            sum += number;
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    检查文件

    在C++中可以使用文件流对象的方法和标志来进行文件检查以确保文件操作的正确性。

    • 使用 is_open() 方法:is_open() 方法用于检查文件是否成功打开。如果文件成功打开,它将返回 true;否则,返回 false。
    std::ifstream inFile("example.txt");
    if (inFile.is_open()) {
        // 文件已成功打开,可以进行读取操作
    } else {
        // 文件打开失败,进行错误处理
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 使用 good() 方法:good() 方法用于检查文件流的状态是否有效。如果文件流处于有效状态,它将返回 true;否则,返回 false。
    std::ifstream inFile("example.txt");
    if (inFile.good()) {
        // 文件流处于有效状态,可以进行读取操作
    } else {
        // 文件流处于无效状态,进行错误处理
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 使用 fail() 方法:fail() 方法用于检查文件是否发生了失败状态,例如,当试图读取一个无效类型的数据时。如果文件发生了失败状态,它将返回 true;否则,返回 false。
    std::ifstream inFile("example.txt");
    if (inFile.fail()) {
        // 文件操作失败,进行错误处理
    } else {
        // 文件操作成功,可以进行读取操作
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    读取和写入实例

    #include 
    #include 
    
    int main(){
        //打开输入文件以读取数据
        std::ifstream inputFile("input.txt");
        if (!inputFile.is_open()){
            std::cerr << "Failed to open the input file."<< std::endl;
            return 1;
        }
        int sum = 0;
        int number;
    
        // 从输入文件中读取整数并计算他们的和
        while (inputFile >> number){
            sum += number;
        }
    
        inputFile.close();
    
        //打开输出文件以写入结果
        std::ofstream outputFile("output.txt");
        if (!outputFile.is_open()){
            std::cerr << "Failed to open the output file." << std::endl;
            return 1;
        }
    
        // 将计算结果写入输出文件
        outputFile << "Sum of numbers in the input file: " << sum << std::endl;
    
        outputFile.close(); //关闭输出文件
    
        std::cout << "Sum has been written to the output file." << std::endl;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
  • 相关阅读:
    K8S核心概念之SVC(易混淆难理解知识点总结)
    指派问题——匈牙利法
    【PAT甲级 - C++题解】1048 Find Coins
    XSS线上靶场---haozi
    ubuntu20.04编译osg
    自动保存恢复tmux会话 关机重启再也不怕
    IRC/ML:金融智能风控—信贷风控场景简介、两大场景(贷款场景+信用卡场景)、信用卡评分模型设计、反欺诈检测技术的简介、案例应用之详细攻略
    算法练习8——有序三元组中的最大值
    泛型类和泛型方法
    颜色扩散类dp及其优化:0919T2
  • 原文地址:https://blog.csdn.net/Sciurdae/article/details/133849249