C++ - 文件操作

fstream是 C++ 标准库中用于文件操作的头文件,提供了文件输入输出流的功能,可以创建文件流对象并使用它们来读取和写入文件的内容。

#include <fstream>

一、文件输入流(std::ifstream)

1、构造函数
  1. ifstream::ifstream():默认构造函数,创建一个未关联到任何文件的输入文件流对象;
  2. ifstream::ifstream(const char* filename):带参数的构造函数,创建一个与指定文件关联的输入文件流对象。
2、打开/关闭文件
  1. void ifstream::open(const char* filename, ios_base::openmode mode = ios_base::in):打开指定文件,并指定打开模式(默认为只读);
  2. void ifstream::close():关闭文件。
3、读取文件内容
  1. bool ifstream::is_open():检查文件是否成功打开;
  2. bool ifstream::good():检查文件是否处于正常状态(即没有错误发生);
  3. bool ifstream::eof():检查是否已到达文件末尾;
  4. void ifstream::clear():清除流的错误状态标志;
  5. char ifstream::get():从文件中读取一个字符;
  6. string ifstream::getline(string& str):从文件中读取一行数据到字符串中;
  7. istream& ifstream::operator>>(type& val):从文件中读取指定类型的数据。
4、位置和状态
  1. streampos ifstream::tellg():获取当前读取位置;
  2. void ifstream::seekg(streampos pos):设置读取位置到指定位置;
  3. void ifstream::seekg(streamoff off, ios_base::seekdir way):根据指定偏移量和方式设置读取位置。
5、示例代码
#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::ifstream inFile("example.txt"); // 创建一个名为example.txt 的输入文件流对象

    if (inFile.is_open()) { // 检查文件是否成功打开
        std::string line;
        while (std::getline(inFile, line)) { // 逐行读取文件内容
            std::cout << line << std::endl; // 输出每行内容到控制台
        }
        inFile.close(); // 关闭文件流
        std::cout << "File successfully read." << std::endl;
    } else {
        std::cout << "Error opening file!" << std::endl;
        return 1;
    }

    return 0;
}

二、文件输出流(std::ofstream)

1、构造函数
  1. ofstream(): 默认构造函数,创建一个未关联到任何文件的输出文件流;
  2. explicit ofstream(const char* filename, ios_base::openmode mode = ios_base::out): 构造函数,打开一个文件以供写入,参数 filename 是要打开的文件名,mode 是打开文件的模式,默认为输出模式。
2、打开/关闭文件
  1. void open(const char* filename, ios_base::openmode mode = ios_base::out): 打开一个文件以供写入。参数与构造函数相同;
  2. void close(): 关闭文件流,释放关联的资源。
3、写入文件内容
  1. bool is_open() const: 检查文件流是否已经成功打开一个文件;
  2. bool good() const: 检查流是否处于良好状态,即没有错误发生;
  3. void clear(): 清除流的错误状态;
  4. bool fail() const: 检查流是否发生了错误,但错误可以被清除;
  5. bool bad() const: 检查流是否发生了不可恢复的错误;
  6. ofstream& operator<<(T val): 将数据 val 写入文件流中,支持各种数据类型的写入操作;
  7. ostream& write(const char* s, streamsize n): 将字符数组 s 中的 n 个字节写入文件流中。
4、位置和状态
  1. pos_type tellp(): 返回当前写入位置的文件指针位置。
  2. ostream& seekp(pos_type pos): 设置文件指针位置,用于移动写入位置。
  3. ostream& seekp(off_type off, ios_base::seekdir way): 以指定方式移动文件指针位置。
5、示例代码
#include <iostream>
#include <fstream>

int main() {
    // 创建 ofstream 对象并以追加模式打开文件
    std::ofstream outputFile("example.txt", std::ios::app);

    // 检查文件是否成功打开
    if (outputFile.is_open()) {
        // 写入数据到文件
        outputFile << "This is additional text appended to the file.\n";
        outputFile << "Appending some numbers: " << 456 << " " << 2.718 << "\n";
        
        // 关闭文件
        outputFile.close();
        std::cout << "File append operation successful.\n";
    } else {
        // 文件打开失败
        std::cerr << "Unable to open file.\n";
    }

    return 0;
}

三、文件输入/输出流(std::fstream)

1、构造函数
  1. fstream(): 默认构造函数,创建一个未关联到任何文件的文件流;
  2. explicit fstream(const char* filename, ios_base::openmode mode = ios_base::in | ios_base::out): 构造函数,打开一个文件以供读写,参数 filename 是要打开的文件名,mode 是打开文件的模式,默认为输入输出模式。
2、打开/关闭文件
  1. void open(const char* filename, ios_base::openmode mode = ios_base::in | ios_base::out): 打开一个文件以供读写,参数与构造函数相同;
  2. void close(): 关闭文件流,释放关联的资源。
3、读写文件内容
  1. operator>>: 从文件流中提取数据;
  2. operator<<: 向文件流中插入数据;
  3. istream& getline(char* s, streamsize n): 从文件流中读取一行数据;
  4. ostream& write(const char* s, streamsize n): 将字符数组 s 中的 n 个字节写入文件流中。
4、位置和状态
  1. pos_type tellg(): 返回当前读取位置的文件指针位置;
  2. istream& seekg(pos_type pos): 设置文件指针位置,用于移动读取位置;
  3. istream& seekg(off_type off, ios_base::seekdir way): 以指定方式移动文件指针位置;
  4. pos_type tellp(): 返回当前写入位置的文件指针位置;
  5. ostream& seekp(pos_type pos): 设置文件指针位置,用于移动写入位置;
  6. ostream& seekp(off_type off, ios_base::seekdir way): 以指定方式移动文件指针位置。
5、示例代码
#include <iostream>
#include <fstream>
#include <string>

int main() {
    // 创建 fstream 对象并打开文件
    std::fstream file("example.txt", std::ios::out | std::ios::in);

    // 检查文件是否成功打开
    if (file.is_open()) {
        // 写入数据到文件
        file << "This is a sample text.\n";
        file << "Writing some numbers: " << 123 << " " << 3.14 << "\n";
        
        // 将文件指针移动到文件开头
        file.seekg(0, std::ios::beg);

        // 读取文件内容并打印到控制台
        std::string line;
        while (std::getline(file, line)) { // 确保使用std::getline函数
            std::cout << "Line from file: " << line << "\n";
        }

        // 关闭文件
        file.close();
        std::cout << "File read/write operation successful.\n";
    } else {
        // 文件打开失败
        std::cerr << "Unable to open file.\n";
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值