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;
}
相关推荐
郝学胜_神的一滴10 小时前
CMake 034:生成器表达式:解耦构建时序、精简分支逻辑的终极利器
c++·cmake
见过夏天1 天前
C++ 基础入门完全指南
c++
用户805533698032 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
BadBadBad__AK3 天前
线段树维护区间 k 次方和
c++·数学·算法·stl
卷无止境3 天前
Eigen 库如何借助 OpenMP 加速计算
c++·后端
卷无止境3 天前
OpenMPI、MPICH 与 OpenMP:关系、核心概念与架构全解
c++·后端
郝学胜_神的一滴4 天前
CMake 30:循环语法全解|foreach_while双循环精讲、迭代技巧与实战避坑指南
c++·cmake
卷无止境6 天前
C++ 的Eigen 库全解析
c++
卷无止境6 天前
现代 C++特性大盘点:一门脱胎换骨的老语言
c++·后端
郝学胜_神的一滴6 天前
CMake 27:缓存变量的特性、语法、类型与实操全解
c++·cmake