C++ 入门第26天:文件与流操作基础

往期回顾:

C++ 入门第23天:Lambda 表达式与标准库算法入门-优快云博客

C++ 入门第24天:C++11 多线程基础-优快云博客

C++ 入门第25天:线程池(Thread Pool)基础-优快云博客


 C++ 入门第26天:文件与流操作基础

前言

文件是程序中用来存储数据的常用工具。在 C++ 中,文件操作是通过流(Stream)来实现的。C++ 提供了一组标准库类(如 ifstreamofstreamfstream)用于文件读写操作。

今天,我们将学习如何使用这些工具进行文件的读写,以及处理文件操作中的一些常见问题。

1. 文件流的基本概念

在 C++ 中,文件操作是通过以下三种流来实现的:

  1. ifstream:输入文件流,用于读取文件。
  2. ofstream:输出文件流,用于写入文件。
  3. fstream:文件流,可同时用于读写文件。

文件流需要头文件 <fstream>

2. 写入文件

示例代码

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ofstream outfile("example.txt"); // 打开文件以写入

    if (!outfile) {
        cerr << "Error: Unable to open file for writing!" << endl;
        return 1;
    }

    // 写入内容
    outfile << "Hello, C++ File Operations!" << endl;
    outfile << "This is a second line." << endl;

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

    cout << "File written successfully!" << endl;
    return 0;
}

注:

  • ofstream outfile("example.txt");:以写模式打开 example.txt 文件。如果文件不存在,将自动创建。
  • outfile << "内容";:将数据写入文件。
  • outfile.close();:关闭文件,释放资源。

运行结果: 程序运行后,将在当前目录下生成一个名为 example.txt 的文件,文件内容为:

Hello, C++ File Operations!
This is a second line.

3. 读取文件

示例代码

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ifstream infile("example.txt"); // 打开文件以读取

    if (!infile) {
        cerr << "Error: Unable to open file for reading!" << endl;
        return 1;
    }

    string line;

    // 按行读取文件
    while (getline(infile, line)) {
        cout << line << endl;
    }

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

    return 0;
}

注:

  • ifstream infile("example.txt");:以读模式打开 example.txt 文件。
  • getline(infile, line);:按行读取文件内容。
  • infile.close();:关闭文件。

运行结果: 程序将输出文件 example.txt 的内容:

Hello, C++ File Operations!
This is a second line.

4. 同时读写文件

使用 fstream 类可以同时对文件进行读写操作。

示例代码

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    fstream file("example.txt", ios::in | ios::out | ios::app); // 以读写追加模式打开文件

    if (!file) {
        cerr << "Error: Unable to open file!" << endl;
        return 1;
    }

    // 写入新内容
    file << "Appending a new line to the file." << endl;

    // 将文件指针移到文件开始位置
    file.seekg(0, ios::beg);

    // 读取文件内容
    string line;
    while (getline(file, line)) {
        cout << line << endl;
    }

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

    return 0;
}

 

  • ios::in:读模式。
  • ios::out:写模式。
  • ios::app:追加模式,将写入内容添加到文件末尾。
  • file.seekg(0, ios::beg);:将文件指针移动到文件开头,以便读取文件内容。

5. 文件操作常见问题

5.1 检查文件是否存在

#include <fstream>
#include <iostream>
using namespace std;

int main() {
    ifstream infile("example.txt");

    if (infile) {
        cout << "File exists!" << endl;
    } else {
        cout << "File does not exist!" << endl;
    }

    infile.close();
    return 0;
}

5.2 删除文件

C++ 提供了 remove 函数用于删除文件。

#include <cstdio> // 包含 remove 函数
#include <iostream>
using namespace std;

int main() {
    if (remove("example.txt") == 0) {
        cout << "File deleted successfully!" << endl;
    } else {
        perror("Error deleting file");
    }
    return 0;
}

6. 总结

以上就是 C++ 11 中文件与流操作的基础知识点了。文件流的类型ifstreamofstreamfstream文件读写操作:如何打开文件、写入内容、读取内容。文件操作技巧:检查文件是否存在和删除文件。文件操作是开发中必不可少的技能,可以用于日志记录、配置文件处理等多种场景。在实际应用中,还需要注意文件路径、权限和异常处理等问题。

都看到这里了,点个赞再走呗朋友~

加油吧,预祝大家变得更强!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值