首页 > 后端开发 > C++ > 正文

c++怎么读取和写入文件_c++文件读写操作完整教程

尼克
发布: 2025-11-05 17:41:02
原创
779人浏览过
C++文件操作需包含fstream头文件,使用ofstream写入、ifstream读取、fstream读写文件,支持文本和二进制模式,需检查文件是否打开并选择合适模式。

c++怎么读取和写入文件_c++文件读写操作完整教程

在C++中进行文件读写操作,主要使用标准库中的 fstream 头文件。它提供了三个核心类:

  • ifstream:用于从文件读取数据(input file stream)
  • ofstream:用于向文件写入数据(output file stream)
  • fstream:同时支持读写操作

下面通过具体示例详细讲解如何使用这些类完成常见的文件操作。

1. 包含头文件

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

这三个头文件分别用于文件操作、输入输出和字符串处理。

2. 写入文件(ofstream)

使用 ofstream 向文件中写入内容。如果文件不存在会自动创建;如果已存在,默认会覆盖原内容。

立即学习C++免费学习笔记(深入)”;

std::ofstream file("example.txt");
if (file.is_open()) {
    file << "Hello, C++ File!" << std::endl;
    file << "This is line two." << std::endl;
    file.close();
} else {
    std::cout << "无法打开文件!" << std::endl;
}

你也可以以追加模式写入,避免覆盖原内容:

std::ofstream file("example.txt", std::ios::app);
file << "追加的新行" << std::endl;

3. 读取文件(ifstream)

使用 ifstream 从文件读取内容。可以逐行读取或按单词读取。

std::ifstream file("example.txt");
std::string line;
if (file.is_open()) {
    while (getline(file, line)) {
        std::cout << line << std::endl;
    }
    file.close();
} else {
    std::cout << "无法打开文件!" << std::endl;
}

注意:getline(file, line) 每次读取一行,直到遇到换行符为止。

小绿鲸英文文献阅读器
小绿鲸英文文献阅读器

英文文献阅读器,专注提高SCI阅读效率

小绿鲸英文文献阅读器 199
查看详情 小绿鲸英文文献阅读器

4. 同时读写文件(fstream)

当你需要对同一个文件进行读写操作时,使用 fstream 类,并指定模式。

std::fstream file("example.txt", std::ios::in | std::ios::out);
// 先读取所有内容
std::string content;
while (getline(file, content)) {
    std::cout << content << std::endl;
}
// 移动指针到末尾再写入
file << "新增内容" << std::endl;

常见打开模式:

  • std::ios::in:读取
  • std::ios::out:写入(默认会清空文件)
  • std::ios::app:追加
  • std::ios::ate:打开后立即定位到文件末尾
  • std::ios::binary:二进制模式

5. 检查文件是否存在

可以通过尝试打开文件来判断是否存在:

std::ifstream file("example.txt");
if (!file) {
    std::cout << "文件不存在!" << std::endl;
} else {
    std::cout << "文件存在。" << std::endl;
    file.close();
}

6. 二进制文件读写

对于非文本文件(如图片、音频),应使用二进制模式。

// 写入二进制
std::ofstream out("data.bin", std::ios::binary);
int data = 12345;
out.write(reinterpret_cast<const char*>(&data), sizeof(data));
out.close();
// 读取二进制
std::ifstream in("data.bin", std::ios::binary);
int value;
in.read(reinterpret_cast<char*>(&value), sizeof(value));
in.close();
std::cout << "读取的值:" << value << std::endl;

使用 read()write() 函数处理原始字节数据。

7. 完整示例:读写学生信息

#include <iostream>
#include <fstream>
#include <string>
int main() {
    // 写入数据
    std::ofstream out("students.txt");
    if (out.is_open()) {
        out << "张三 20\n";
        out << "李四 22\n";
        out.close();
    }
    // 读取数据
    std::ifstream in("students.txt");
    std::string name;
    int age;
    if (in.is_open()) {
        while (in >> name >> age) {
            std::cout << "姓名:" << name << ", 年龄:" << age << std::endl;
        }
        in.close();
    }
    return 0;
}

基本上就这些。掌握这些方法后,就能处理大多数C++文件操作任务了。关键是记得检查文件是否成功打开,并根据需求选择合适的模式。不复杂但容易忽略细节。

以上就是c++++怎么读取和写入文件_c++文件读写操作完整教程的详细内容,更多请关注php中文网其它相关文章!

c++速学教程(入门到精通)
c++速学教程(入门到精通)

c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号