一文詳解C++中打開文件的多種方式及相關(guān)流類
前言
打開一個文件的方式都有哪些,都可以通過哪些流來獲取它?
在 C++ 中,打開文件可以通過多種流類實現(xiàn),每種方式都有其特定的用途和特點。以下是詳細的分類和說明:
1. 使用標準文件流類(<fstream>)
1.1ofstream- 輸出文件流(寫入)
#include <fstream>
// 方式1:構(gòu)造函數(shù)直接打開
std::ofstream outFile1("output.txt"); // 默認模式:ios::out | ios::trunc
// 方式2:先創(chuàng)建后打開
std::ofstream outFile2;
outFile2.open("output.txt", std::ios::app); // 追加模式
// 寫入數(shù)據(jù)
outFile1 << "Hello World" << std::endl;1.2ifstream- 輸入文件流(讀?。?/h3>
#include <fstream>
// 方式1:構(gòu)造函數(shù)直接打開
std::ifstream inFile1("input.txt"); // 默認模式:ios::in
// 方式2:先創(chuàng)建后打開
std::ifstream inFile2;
inFile2.open("input.txt");
// 讀取數(shù)據(jù)
std::string line;
while (std::getline(inFile2, line)) {
std::cout << line << std::endl;
}
#include <fstream>
// 方式1:構(gòu)造函數(shù)直接打開
std::ifstream inFile1("input.txt"); // 默認模式:ios::in
// 方式2:先創(chuàng)建后打開
std::ifstream inFile2;
inFile2.open("input.txt");
// 讀取數(shù)據(jù)
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 風(fēng)格文件操作(<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); // 必須手動關(guān)閉
}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); // 寫入+創(chuàng)建
if (fd != -1) {
char buffer[1024];
read(fd, buffer, sizeof(buffer));
close(fd); // 必須手動關(guān)閉
}4. 使用內(nèi)存映射文件(高級方式)
#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);
// 使用映射的內(nèi)存區(qū)域...
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 風(fēng)格打開模式
| 模式 | 描述 |
|---|---|
| "r" | 只讀 |
| "w" | 只寫(創(chuàng)建/清空) |
| "a" | 追加寫入 |
| "r+" | 讀寫 |
| "w+" | 讀寫(創(chuàng)建/清空) |
| "a+" | 讀和追加 |
6. 不同方式的比較
| 特性 | C++ 流 | C 文件操作 | 文件描述符 |
|---|---|---|---|
| 面向?qū)ο?/td> | ? | ? | ? |
| 類型安全 | ? | ? | ? |
| 自動資源管理 | ? | ? | ? |
| 格式化I/O | ? | ? | ? |
| 二進制I/O | ? | ? | ? |
| 低級控制 | ? | 部分 | ? |
| 跨平臺 | ? | ? | 基本? |
| 性能 | 中等 | 較高 | 最高 |
7. 最佳實踐建議
常規(guī)文本文件操作:優(yōu)先使用 C++ 文件流(fstream/ofstream/ifstream)
高性能需求:考慮使用 C 風(fēng)格文件操作或文件描述符
二進制文件:總是使用 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) {
// 讀取當(dāng)前位置
std::streampos endPos = file.tellg();
// 回到開頭讀取
file.seekg(0);
std::string content;
std::getline(file, content);
// 寫入新數(shù)據(jù)
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 風(fēng)格和文件描述符則提供了更多的控制和更高的性能。
到此這篇關(guān)于一文詳解C++中打開文件的多種方式及相關(guān)流類的文章就介紹到這了,更多相關(guān)C++打開文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于WTL 雙緩沖(double buffer)繪圖的分析詳解
本篇文章是對WTL下使用雙緩沖(double buffer)繪圖進行了詳細的分析介紹,需要的朋友參考下2013-05-05
C/C++表格組件Qt?TableWidget應(yīng)用詳解
本文詳細講解了C/C++中使用列表框組件Qt?TableWidget的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12

