C++ 中打开文件的多种方式及相关流类
C++学习:
https://blog.ZEEKLOG.net/2301_80220607/category_12805124.html?spm=1001.2014.3001.5482
前言:
打开一个文件的方式都有哪些,都可以通过哪些流来获取它?
在 C++ 中,打开文件可以通过多种流类实现,每种方式都有其特定的用途和特点。以下是详细的分类和说明:
目录
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. 最佳实践建议
- 常规文本文件操作:优先使用 C++ 文件流(
fstream/ofstream/ifstream) - 高性能需求:考虑使用 C 风格文件操作或文件描述符
- 二进制文件:总是使用
std::ios::binary标志 - 资源管理:使用 RAII 原则,推荐 C++ 流自动管理资源
错误检查:始终检查文件是否成功打开
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 风格和文件描述符则提供了更多的控制和更高的性能。