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. 使用标准文件流类()
1.1 ofstream
- 输出文件流(写入)
#include // 方式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 // 方式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 // 读写模式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 风格文件操作()
#include // 打开方式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 #include 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 #include #include 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 风格打开模式
6. 不同方式的比较
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(&rec), sizeof(Record));// 读取二进制std::ifstream binIn(\"data.bin\", std::ios::binary);Record inRec;binIn.read(reinterpret_cast(&inRec), sizeof(Record));
选择哪种方式取决于具体需求,C++ 文件流提供了最安全和方便的方式,而 C 风格和文件描述符则提供了更多的控制和更高的性能。