当前位置: 首页 > news >正文

C++ 中打开文件的多种方式及相关流类

C++学习:

https://blog.csdn.net/2301_80220607/category_12805124.html?spm=1001.2014.3001.5482

前言:

打开一个文件的方式都有哪些,都可以通过哪些流来获取它?

在 C++ 中,打开文件可以通过多种流类实现,每种方式都有其特定的用途和特点。以下是详细的分类和说明:

目录

1. 使用标准文件流类()

1.1 ofstream - 输出文件流(写入)

1.2 ifstream - 输入文件流(读取)

1.3 fstream - 双向文件流(读写)

2. 使用 C 风格文件操作()

3. 使用文件描述符(POSIX/unistd.h)

4. 使用内存映射文件(高级方式)

5. 文件打开模式详解

5.1 C++ 流打开模式(位掩码组合)

5.2 C 风格打开模式

6. 不同方式的比较

7. 最佳实践建议

8. 高级用法示例

8.1 同时读写文件

8.2 二进制文件操作


1. 使用标准文件流类(<fstream>)

1.1 ofstream - 输出文件流(写入)

#include <fstream>// 方式1:构造函数直接打开
std::ofstream outFile1("output.txt"); // 默认模式:ios::out | ios::trunc// 方式2:先创建后打开
std::ofstream outFile2;
outFile2.open("output.txt", std::ios::app); // 追加模式// 写入数据
outFile1 << "Hello World" << std::endl;

1.2 ifstream - 输入文件流(读取)

#include <fstream>// 方式1:构造函数直接打开
std::ifstream inFile1("input.txt"); // 默认模式:ios::in// 方式2:先创建后打开
std::ifstream inFile2;
inFile2.open("input.txt");// 读取数据
std::string line;
while (std::getline(inFile2, line)) {std::cout << line << std::endl;
}

1.3 fstream - 双向文件流(读写)

#include <fstream>// 读写模式
std::fstream ioFile("data.txt", std::ios::in | std::ios::out);// 读写二进制文件
std::fstream binFile("data.bin", std::ios::binary | std::ios::in | std::ios::out);

2. 使用 C 风格文件操作(<cstdio>)

#include <cstdio>// 打开方式
FILE* file = fopen("file.txt", "r"); // 读取
FILE* file = fopen("file.txt", "w"); // 写入(清空)
FILE* file = fopen("file.txt", "a"); // 追加
FILE* file = fopen("file.bin", "rb"); // 二进制读取if (file != nullptr) {// 使用文件...fclose(file); // 必须手动关闭
}

3. 使用文件描述符(POSIX/unistd.h)

#include <unistd.h>
#include <fcntl.h>int fd = open("file.txt", O_RDONLY); // 只读
int fd = open("file.txt", O_WRONLY | O_CREAT, 0644); // 写入+创建if (fd != -1) {char buffer[1024];read(fd, buffer, sizeof(buffer));close(fd); // 必须手动关闭
}

4. 使用内存映射文件(高级方式)

#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>int fd = open("largefile.bin", O_RDONLY);
void* mapped = mmap(nullptr, file_size, PROT_READ, MAP_PRIVATE, fd, 0);// 使用映射的内存区域...
munmap(mapped, file_size);
close(fd);

5. 文件打开模式详解

5.1 C++ 流打开模式(位掩码组合)

模式标志描述
std::ios::in读取模式
std::ios::out写入模式
std::ios::app追加写入
std::ios::ate打开后定位到文件末尾
std::ios::trunc如果文件存在则清空
std::ios::binary二进制模式

5.2 C 风格打开模式

模式描述
"r"只读
"w"只写(创建/清空)
"a"追加写入
"r+"读写
"w+"读写(创建/清空)
"a+"读和追加

6. 不同方式的比较

特性C++ 流C 文件操作文件描述符
面向对象
类型安全
自动资源管理
格式化I/O
二进制I/O
低级控制部分
跨平台基本✓
性能中等较高最高

7. 最佳实践建议

  1. 常规文本文件操作:优先使用 C++ 文件流(fstream/ofstream/ifstream

  2. 高性能需求:考虑使用 C 风格文件操作或文件描述符

  3. 二进制文件:总是使用 std::ios::binary 标志

  4. 资源管理:使用 RAII 原则,推荐 C++ 流自动管理资源

  5. 错误检查:始终检查文件是否成功打开

    std::ofstream file("data.txt");
    if (!file.is_open()) {// 错误处理
    }

8. 高级用法示例

8.1 同时读写文件

std::fstream file("data.txt", std::ios::in | std::ios::out |std::ios::ate); // 打开并定位到末尾if (file) {// 读取当前位置std::streampos endPos = file.tellg();// 回到开头读取file.seekg(0);std::string content;std::getline(file, content);// 写入新数据file << "\nNew data appended";
}

8.2 二进制文件操作

struct Record {int id;char name[50];double value;
};// 写入二进制
std::ofstream binOut("data.bin", std::ios::binary);
Record rec{1, "Test", 3.14};
binOut.write(reinterpret_cast<char*>(&rec), sizeof(Record));// 读取二进制
std::ifstream binIn("data.bin", std::ios::binary);
Record inRec;
binIn.read(reinterpret_cast<char*>(&inRec), sizeof(Record));

选择哪种方式取决于具体需求,C++ 文件流提供了最安全和方便的方式,而 C 风格和文件描述符则提供了更多的控制和更高的性能。

http://www.dtcms.com/a/296374.html

相关文章:

  • matplotlib的详细知识点
  • k8s之ingress定义https访问方式
  • 【AI News | 20250723】每日AI进展
  • Windows11 本地安装docker Desktop 部署dify 拉取镜像报错
  • iOS Core Data 本地数据库 使用详解:从模型关系到数据操作
  • 技嘉z370主板开启vtx
  • 谈谈ArrayList与Vector的理解?
  • SpringBoot+AI+Web3实战指南
  • Python循环结构
  • 红黑树:高效平衡的终极指南
  • c语言学习(dyas10)
  • Kubernetes Kubelet 资源配置优化指南:从命令行参数到配置文件的最佳实践
  • Spring AI - ChatModel接口演示
  • TCO,UDP考点
  • 开发避坑短篇(5):vue el-date-picker 设置默认开始结束时间
  • SpringBoot航空订票系统的设计与实现
  • 视频模型国产PK国外?
  • 金仓数据库:从国产替代到AI融合的破局之路
  • #来昇腾学AI 【十天成长计划】大模型LLM Prompt初级班
  • Linux的工具
  • 提取边界线的思路与原理
  • Linux---systemd自启动
  • 论文复现-windows电脑在pycharm中运行.sh文件
  • 嵌入式——C语言:函数②
  • webGis框架
  • 元计算推动产业元宇宙改变世界
  • 将Scrapy项目容器化:Docker镜像构建的工程实践
  • Web前端开发:JavaScript reduce() 方法
  • 借助AI学习开源代码git0.7之九diff-files
  • MCU中的系统总线