前言
打开一个文件的方式都有哪些,都可以通过哪些流来获取它?
在 c++ 中,打开文件可以通过多种流类实现,每种方式都有其特定的用途和特点。以下是详细的分类和说明:
1. 使用标准文件流类(<fstream>)
1.1ofstream- 输出文件流(写入)
#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.2ifstream- 输入文件流(读取)
#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.3fstream- 双向文件流(读写)
#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 风格和文件描述符则提供了更多的控制和更高的性能。
到此这篇关于一文详解c++中打开文件的多种方式及相关流类的文章就介绍到这了,更多相关c++打开文件内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论