系列目錄
上一篇:白騎士的C++教學進階篇 2.4 標準模板庫(STL)
????????文件操作是C++編程中的一個重要部分,允許程序與外部存儲設備進行交互,從而實現數據的持久化存儲和讀取。C++標準庫提供了豐富的文件操作功能,包括文件的讀寫、文件流和二進制文件操作。本篇博客將詳細介紹這些內容,幫助您掌握C++文件操作的基本技巧。
文件的讀寫
????????C++標準庫中的文件讀寫通過文件流(file stream)實現。文件流分為輸入文件流(ifstream)和輸出文件流(ofstream),分別用于從文件讀取數據和向文件寫入數據。
文件寫入
????????首先,讓我們看看如何使用‘ofstream‘類向文件寫入數據。‘ofstream‘類用于創建文件并寫入數據。如果文件不存在,它會創建文件;如果文件已經存在,它會覆蓋文件內容。例如:
#include <iostream>
#include <fstream>
#include <string>int main() {std::ofstream outFile("example.txt");if (outFile.is_open()) {outFile << "Hello, World!" << std::endl;outFile << "This is a test file." << std::endl;outFile << "C++ file operations are easy to learn." << std::endl;outFile.close();std::cout << "Data written to file successfully." << std::endl;} else {std::cerr << "Unable to open file for writing." << std::endl;}return 0;
}
????????在上面的代碼中,我們使用‘ofstream‘創建了一個名為‘example.txt‘的文件,并向文件中寫入了幾行文本。最后關閉文件流。
文件讀取
????????接下來,讓我們看看如何使用 ‘ifstream‘ 類從文件讀取數據。‘ifstream‘ 類用于打開文件并讀取數據,例如:
#include <iostream>
#include <fstream>
#include <string>int main() {std::ifstream inFile("example.txt");std::string line;if (inFile.is_open()) {while (getline(inFile, line)) {std::cout << line << std::endl;}inFile.close();} else {std::cerr << "Unable to open file for reading." << std::endl;}return 0;
}
????????在上面的代碼中,我們使用 ‘ifstream‘ 打開了一個名為 ‘example.txt‘ 的文件,并逐行讀取文件內容,打印到標準輸出。
文件流
????????C++標準庫提供了一個更通用的文件流類——‘fstream‘,它既可以用于文件讀取,也可以用于文件寫入。通過指定文件流的打開模式,可以實現不同的文件操作需求。
文件流的打開模式
????????‘fstream‘類的打開模式包括:
- ios::in:打開文件用于讀取。
- ios::out:打開文件用于寫入。
- ios::app:打開文件用于追加寫入。
- ios::ate:文件打開后定位到文件末尾。
- ios::trunc:如果文件存在,打開時清空文件內容。
- ios::binary:以二進制模式打開文件。
示例代碼
????????下面的示例代碼展示了如何使用‘fstream‘類同時進行文件的讀寫操作:
#include <iostream>
#include <fstream>
#include <string>int main() {std::fstream file("example.txt", std::ios::in | std::ios::out | std::ios::app);if (file.is_open()) {// 寫入數據file << "Appending a new line to the file." << std::endl;// 定位到文件開頭file.seekg(0);// 讀取數據std::string line;while (getline(file, line)) {std::cout << line << std::endl;}file.close();} else {std::cerr << "Unable to open file." << std::endl;}return 0;
}
????????在上面的代碼中,我們使用 ‘fstream‘ 類打開了一個名為 ‘example.txt‘ 的文件,以讀取、寫入和追加模式打開文件。首先,我們向文件中追加了一行文本,然后定位到文件開頭,并逐行讀取文件內容,打印到標準輸出。
二進制文件操作
????????除了處理文本文件,C++還可以處理二進制文件。二進制文件操作通常用于存儲非文本數據,如圖像、音頻、視頻等。與文本文件不同,二進制文件的讀寫操作必須使用二進制模式。
二進制文件寫入
????????下面的示例代碼展示了如何使用‘ofstream‘類以二進制模式向文件寫入數據:
#include <iostream>
#include <fstream>int main() {std::ofstream outFile("example.bin", std::ios::binary);if (outFile.is_open()) {int numbers[] = {1, 2, 3, 4, 5};outFile.write(reinterpret_cast<char*>(numbers), sizeof(numbers));outFile.close();std::cout << "Binary data written to file successfully." << std::endl;} else {std::cerr << "Unable to open file for writing." << std::endl;}return 0;
}
????????在上面的代碼中,我們使用 ‘ofstream‘ 類以二進制模式創建了一個名為 ‘example.bin‘ 的文件,并向文件中寫入了一個整數數組。‘reinterpret_cast<char*>(numbers)‘ 用于將整數數組的指針轉換為字符指針,以便寫入二進制數據。
二進制文件讀取
????????接下來,讓我們看看如何使用‘ifstream‘類以二進制模式從文件讀取數據,代碼如下:
#include <iostream>
#include <fstream>int main() {std::ifstream inFile("example.bin", std::ios::binary);if (inFile.is_open()) {int numbers[5];inFile.read(reinterpret_cast<char*>(numbers), sizeof(numbers));for (int num : numbers) {std::cout << num << " ";}std::cout << std::endl;inFile.close();} else {std::cerr << "Unable to open file for reading." << std::endl;}return 0;
}
????????在上面的代碼中,我們使用 ‘ifstream‘ 類以二進制模式打開了一個名為 ‘example.bin‘ 的文件,并從文件中讀取了一個整數數組。‘reinterpret_cast<char*>(numbers)‘ 用于將整數數組的指針轉換為字符指針,以便讀取二進制數據。
總結
????????文件操作是C++編程中的一個重要部分,通過文件的讀寫、文件流和二進制文件操作,程序可以實現數據的持久化存儲和讀取。掌握這些基本技巧,將能夠處理各種類型的文件,滿足不同的編程需求。希望通過本篇博客的介紹,你能更好地理解和應用C++的文件操作,為編寫復雜和高效的C++程序打下堅實的基礎。
下一篇:白騎士的C++教學高級篇 3.2 多線程與并發???????