c++ 文件及基本读写总结

在 C++ 中,文件操作是非常重要的一部分,主要用于将数据存储到文件中,或者从文件中读取数据。C++ 标准库提供了fstream头文件,其中包含了用于文件操作的类,主要有ifstream(用于输入文件流,即从文件读取数据)、ofstream(用于输出文件流,即向文件写入数据)和fstream(既可以用于读取也可以用于写入)。以下是关于 C++ 文件基本读写的详细总结:
1. 包含头文件

在使用文件操作之前,需要包含fstream头文件。

cpp 复制代码
#include <fstream>

2. 打开文件

在进行文件读写操作之前,需要先打开文件。可以使用open函数或者在构造函数中指定文件名来打开文件。

示例代码

cpp 复制代码
#include <iostream>
#include <fstream>

int main() {
    // 使用构造函数打开文件
    std::ofstream outFile("example.txt");
    if (!outFile.is_open()) {
        std::cerr << "无法打开文件!" << std::endl;
        return 1;
    }
    // 使用open函数打开文件
    std::ifstream inFile;
    inFile.open("example.txt");
    if (!inFile.is_open()) {
        std::cerr << "无法打开文件!" << std::endl;
        return 1;
    }
    // 关闭文件
    outFile.close();
    inFile.close();
    return 0;
}

3. 文件打开模式

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

  • ios::in:以输入模式打开文件,用于读取数据。
  • ios::out:以输出模式打开文件,用于写入数据。如果文件不存在,则创建文件;如果文件已存在,则清空文件内容。
  • ios::app:以追加模式打开文件,用于在文件末尾追加数据。
  • ios::binary:以二进制模式打开文件。
  • ios::trunc:如果文件已存在,则清空文件内容。
  • ios::ate:打开文件后将文件指针定位到文件末尾。
    示例代码
cpp 复制代码
#include <iostream>
#include <fstream>

int main() {
    // 以追加模式打开文件
    std::ofstream outFile("example.txt", std::ios::app);
    if (!outFile.is_open()) {
        std::cerr << "无法打开文件!" << std::endl;
        return 1;
    }
    outFile.close();
    return 0;
}

4. 写入文件

使用ofstream类可以将数据写入文件,与使用cout输出到控制台类似,使用<<运算符。

示例代码

cpp 复制代码
#include <iostream>
#include <fstream>

int main() {
    std::ofstream outFile("example.txt");
    if (!outFile.is_open()) {
        std::cerr << "无法打开文件!" << std::endl;
        return 1;
    }
    // 写入数据
    outFile << "Hello, World!" << std::endl;
    outFile << 123 << std::endl;
    outFile.close();
    return 0;
}

5. 读取文件

使用ifstream类可以从文件中读取数据,与使用cin从控制台读取数据类似,使用>>运算符。

示例代码

cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::ifstream inFile("example.txt");
    if (!inFile.is_open()) {
        std::cerr << "无法打开文件!" << std::endl;
        return 1;
    }
    std::string line;
    int num;
    // 读取数据
    std::getline(inFile, line);
    inFile >> num;
    std::cout << "读取的字符串: " << line << std::endl;
    std::cout << "读取的整数: " << num << std::endl;
    inFile.close();
    return 0;
}

6. 二进制文件读写

除了文本文件读写,C++ 还支持二进制文件读写。使用ios::binary模式打开文件,使用read和write函数进行读写操作。

示例代码

cpp 复制代码
#include <iostream>
#include <fstream>

struct Person {
    char name[50];
    int age;
};

int main() {
    // 写入二进制文件
    Person p = {"John", 25};
    std::ofstream outFile("person.bin", std::ios::binary);
    if (!outFile.is_open()) {
        std::cerr << "无法打开文件!" << std::endl;
        return 1;
    }
    outFile.write(reinterpret_cast<const char*>(&p), sizeof(Person));
    outFile.close();

    // 读取二进制文件
    Person readPerson;
    std::ifstream inFile("person.bin", std::ios::binary);
    if (!inFile.is_open()) {
        std::cerr << "无法打开文件!" << std::endl;
        return 1;
    }
    inFile.read(reinterpret_cast<char*>(&readPerson), sizeof(Person));
    std::cout << "姓名: " << readPerson.name << std::endl;
    std::cout << "年龄: " << readPerson.age << std::endl;
    inFile.close();
    return 0;
}

outFile.close();

inFile.close();

8. 检查文件状态

在进行文件操作时,可以使用一些函数来检查文件的状态,例如:

  • is_open():检查文件是否成功打开。
  • eof():检查是否到达文件末尾。
  • fail():检查文件操作是否失败。
  • bad():检查文件是否发生严重错误。
    示例代码
cpp 复制代码
#include <iostream>
#include <fstream>

int main() {
    std::ifstream inFile("example.txt");
    if (!inFile.is_open()) {
        std::cerr << "无法打开文件!" << std::endl;
        return 1;
    }
    int num;
    while (inFile >> num) {
        std::cout << num << std::endl;
    }
    if (inFile.eof()) {
        std::cout << "到达文件末尾!" << std::endl;
    }
    if (inFile.fail()) {
        std::cerr << "文件操作失败!" << std::endl;
    }
    inFile.close();
    return 0;
}
相关推荐
我材不敲代码3 小时前
Python实现打包贪吃蛇游戏
开发语言·python·游戏
身如柳絮随风扬4 小时前
Java中的CAS机制详解
java·开发语言
-dzk-5 小时前
【代码随想录】LC 59.螺旋矩阵 II
c++·线性代数·算法·矩阵·模拟
韩立学长5 小时前
【开题答辩实录分享】以《基于Python的大学超市仓储信息管理系统的设计与实现》为例进行选题答辩实录分享
开发语言·python
froginwe116 小时前
Scala 循环
开发语言
m0_706653236 小时前
C++编译期数组操作
开发语言·c++·算法
故事和你916 小时前
sdut-Java面向对象-06 继承和多态、抽象类和接口(函数题:10-18题)
java·开发语言·算法·面向对象·基础语法·继承和多态·抽象类和接口
Bruk.Liu6 小时前
(LangChain实战2):LangChain消息(message)的使用
开发语言·langchain
qq_423233906 小时前
C++与Python混合编程实战
开发语言·c++·算法
m0_715575347 小时前
分布式任务调度系统
开发语言·c++·算法