Loading

c++文件操作

在 C++ 中,文件操作是一种常见的任务,它允许我们读取和写入文件数据。C++ 提供了标准库 <fstream> 来支持文件操作,包括文件的读取、写入和定位等功能。以下是 C++ 中常见的文件操作方法:

文件的打开和关闭

在进行文件操作之前,我们需要先打开文件,并在文件操作完成后关闭文件。打开文件可以使用 ofstream(输出文件流)和 ifstream(输入文件流)类,也可以使用 fstream(文件流)类,具体取决于你要对文件进行读取、写入还是同时读取和写入。

打开文件示例:

#include <fstream>

int main() {
    std::ofstream outputFile;
    outputFile.open("example.txt"); // 打开文件 example.txt 进行写入

    if (!outputFile) {
        std::cout << "Error opening file." << std::endl;
        return 1;
    }

    outputFile << "Hello, World!" << std::endl;
    outputFile.close(); // 关闭文件

    return 0;
}

文件的写入(输出)

使用 ofstream 类进行文件的写入操作。你可以使用输出运算符 << 将数据写入文件。

文件写入示例:

#include <fstream>

int main() {
    std::ofstream outputFile;
    outputFile.open("example.txt"); // 打开文件 example.txt 进行写入

    if (!outputFile) {
        std::cout << "Error opening file." << std::endl;
        return 1;
    }

    outputFile << "Hello, World!" << std::endl;
    outputFile << 42 << std::endl;
    outputFile << 3.14159 << std::endl;

    outputFile.close(); // 关闭文件

    return 0;
}

文件的读取(输入)

使用 ifstream 类进行文件的读取操作。你可以使用输入运算符 >> 从文件中读取数据。

文件读取示例:

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

int main() {
    std::ifstream inputFile;
    inputFile.open("example.txt"); // 打开文件 example.txt 进行读取

    if (!inputFile) {
        std::cout << "Error opening file." << std::endl;
        return 1;
    }

    std::string line;
    int num;
    double pi;

    std::getline(inputFile, line); // 读取一行文本
    inputFile >> num; // 读取整数
    inputFile >> pi; // 读取浮点数

    std::cout << "First line: " << line << std::endl;
    std::cout << "Number: " << num << std::endl;
    std::cout << "Pi: " << pi << std::endl;

    inputFile.close(); // 关闭文件

    return 0;
}

文件的定位

使用 seekgseekp 方法可以定位输入和输出文件流的位置,从而在文件中进行读取或写入操作。

文件定位示例:

#include <fstream>
#include <iostream>

int main() {
    std::fstream file;
    file.open("example.txt", std::ios::in | std::ios::out);

    if (!file) {
        std::cout << "Error opening file." << std::endl;
        return 1;
    }

    // 定位到文件末尾
    file.seekg(0, std::ios::end);
    std::cout << "Current position: " << file.tellg() << std::endl;

    // 定位到文件开头
    file.seekg(0, std::ios::beg);
    std::cout << "Current position: " << file.tellg() << std::endl;

    // 定位到文件中的第 5 个字符(偏移量为 4)
    file.seekg(4);
    std::cout << "Current position: " << file.tellg() << std::endl;

    file.close(); // 关闭文件

    return 0;
}

文件的追加写入

使用 std::ios::app 模式可以实现文件的追加写入,即在文件末尾添加新数据而不覆盖原有内容。

文件追加写入示例:

#include <fstream>
#include <iostream>

int main() {
    std::ofstream outputFile;
    outputFile.open("example.txt", std::ios::app); // 打开文件 example.txt 进行追加写入

    if (!outputFile) {
        std::cout << "Error opening file." << std::endl;
        return 1;
    }

    outputFile << "This line will be appended to the end." << std::endl;
    outputFile.close(); // 关闭文件

    return 0;
}

总结

C++ 中的文件操作提供了强大且方便的方式来读取和写入文件数据。使用 <fstream> 头文件中提供的类和方法,你可以轻松地进行文件操作。在实际编程中,根据具体需求和场景,你可以选择合适的文件流类来处理文件操作任务。

参考资料

  1. C++ Reference: File streams (fstream). https://en.cppreference.com/w/cpp/header/fstream
  2. GeeksforGeeks: File Handling in C++ using ifstream & ofstream classes. https://www.geeksforgeeks.org/file-handling-c-classes/
posted @ 2023-07-18 17:32  ⭐⭐-fighting⭐⭐  阅读(219)  评论(0)    收藏  举报