C++中std::ifstream的使用方法介紹
1. 文件流
為了便于對文件的操作,C++標(biāo)準(zhǔn)庫中提供了文件輸入輸出流fstream,并提供了<fstream>頭文件。
fstream又細(xì)分了兩個分支,分別是處理輸入文件流的ifstream和處理輸出文件流的ofstream。
ifstream負(fù)責(zé)將文件從硬盤讀取至內(nèi)存中。
ofstream負(fù)責(zé)將文件從內(nèi)存寫入硬盤中。
這兩者所處理的文件流均包含二進(jìn)制文件(binary)和文本文件(text)。
接下來我們將針對輸入文件流ifstream用實際的例程介紹其使用方法。
2. 讀取文本
(1)按行讀取文本
#include <fstream> #include <iostream> #include <string> int main() { std::string line; std::ifstream file("example.txt"); // 打開文本文件 if (file.is_open()) { while (getline(file, line)) { // 按行讀取 std::cout << line << '\n'; } file.close(); } else { std::cout << "Unable to open file"; } return 0; }
(2)按字符讀取文本
#include <fstream> #include <iostream> #include <string> int main() { std::ifstream file("example.txt"); // 假設(shè)有一個名為example.txt的文件 if (!file.is_open()) { std::cerr << "Error opening file" << std::endl; return 1; } char ch; while (file.get(ch)) { // 每次讀取一個字符,直到文件末尾 std::cout << ch; } file.close(); // 關(guān)閉文件 return 0; }
(3)逐個字符串讀取文本
#include <fstream> #include <iostream> #include <string> int main() { std::ifstream file("example.txt"); std::string word; /// 默認(rèn)會忽略輸入流中的空白字符,包括空格、制表符、換行符等 while (file >> word) { // 按空白字符分隔讀取每個單詞 std::cout << word << std::endl; } file.close(); return 0; }
(4)逐個數(shù)字讀取文本(適用于文本中存在某類型的數(shù)字)
#include <fstream> #include <iostream> int main() { std::ifstream file("numbers.txt"); int number; /// 默認(rèn)會忽略輸入流中的空白字符,包括空格、制表符、換行符等 while (file >> number) { // 從文件中讀取整數(shù) std::cout << number << std::endl; } file.close(); return 0; }
(5)讀取整個文件到字符串中
#include <fstream> #include <sstream> #include <iostream> #include <string> int main() { std::ifstream file("example.txt"); std::stringstream buffer; buffer << file.rdbuf(); // 讀取整個文件內(nèi)容到buffer std::string contents = buffer.str(); // 將讀取的內(nèi)容轉(zhuǎn)換為字符串 std::cout << contents; file.close(); return 0; }
3. 讀取二進(jìn)制
(1)讀取整個二進(jìn)制文件
#include <fstream> #include <iostream> #include <vector> int main() { // 以二進(jìn)制模式打開文件 std::ifstream file("example.bin", std::ios::binary | std::ios::ate); // std::ios::ate 定位到文件末尾,便于獲取文件大小 if (file.is_open()) { // 獲取文件大小 std::streamsize size = file.tellg(); file.seekg(0, std::ios::beg); // 定位回文件開始 // 分配內(nèi)存緩沖區(qū) std::vector<char> buffer(size); // 讀取文件到緩沖區(qū) if (file.read(buffer.data(), size)) { /* 成功讀取后的處理 */ } else { std::cout << "Error reading file"; } file.close(); } else { std::cout << "Unable to open file"; } return 0; }
(2)分段讀取二進(jìn)制文件
#include <fstream> #include <iostream> #include <vector> int main() { // 打開一個二進(jìn)制文件 std::ifstream file("example.bin", std::ios::binary); if (!file) { std::cerr << "Unable to open file" << std::endl; return 1; } const size_t bufferSize = 1024; // 定義每次讀取的字節(jié)數(shù) char buffer[bufferSize]; // 創(chuàng)建一個緩沖區(qū) // 循環(huán)讀取文件,直到到達(dá)文件末尾 while (!file.eof()) { file.read(buffer, bufferSize); // 嘗試讀取下一段數(shù)據(jù) std::streamsize bytes = file.gcount(); // 獲取實際讀取的字節(jié)數(shù) /// 處理讀取到的數(shù)據(jù)... /// 這里只是簡單地打印出實際讀取的字節(jié)數(shù) std::cout << "Read " << bytes << " bytes" << std::endl; /// 如果需要,可以對buffer中的數(shù)據(jù)進(jìn)行處理 /// 判斷退出條件(此處可避免多讀一次)。 if(file.peek() == EOF){ break; } } file.close(); // 關(guān)閉文件 return 0; }
附:std::ifstream 和std::ofstream 的異同點
std::ifstream 和 std::ofstream 都是C++標(biāo)準(zhǔn)庫中的文件流類,用于進(jìn)行文件的輸入和輸出操作。
異同點如下:
相同點:
都是用于文件操作的流類。
都需要通過文件路徑來創(chuàng)建文件流對象。
都可以使用相同的成員函數(shù)來進(jìn)行文件的讀寫操作。
不同點:
std::ifstream 是輸入文件流類,用于從文件中讀取數(shù)據(jù)。
std::ofstream 是輸出文件流類,用于向文件中寫入數(shù)據(jù)。
std::ifstream 對象創(chuàng)建時需要指定文件路徑,并且只能進(jìn)行讀取操作。
std::ofstream 對象創(chuàng)建時需要指定文件路徑,并且只能進(jìn)行寫入操作。
std::ifstream 對象默認(rèn)以只讀方式打開文件,不能對文件進(jìn)行寫入操作。
std::ofstream 對象默認(rèn)以寫入方式打開文件,不能對文件進(jìn)行讀取操作。
std::ifstream 對象可以使用 >> 運算符來從文件中讀取數(shù)據(jù)。
std::ofstream 對象可以使用 << 運算符來將數(shù)據(jù)寫入文件。
總結(jié)
到此這篇關(guān)于C++中std::ifstream使用方法的文章就介紹到這了,更多相關(guān)C++ std::ifstream使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
華為云開發(fā)工具CodeArts IDE for C/C++開發(fā)使用指南
CodeArts IDE是一個集成開發(fā)環(huán)境(IDE),它提供了開發(fā)語言和調(diào)試服務(wù),本文主要介紹了華為云開發(fā)工具CodeArts IDE for C/C++ 開發(fā)使用指南,感興趣的可以了解一下2023-08-08C語言使用mciSendString實現(xiàn)播放音樂功能
mciSendString?支持?mp3、wma、wav、mid?等多種媒體格式,使用非常簡單。這篇文章就來為大家介紹一下C語言如何使用mciSendString實現(xiàn)播放音樂功能,需要的可以參考一下2023-02-02c語言數(shù)據(jù)結(jié)構(gòu)之棧和隊列詳解(Stack&Queue)
這篇文章主要介紹了c語言數(shù)據(jù)結(jié)構(gòu)之棧和隊列詳解(Stack&Queue),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08使用C++模擬實現(xiàn)2024春晚劉謙魔術(shù)
劉謙在2024年春晚上的撕牌魔術(shù)的數(shù)學(xué)原理非常簡單,所以這篇文章主要為大家詳細(xì)介紹了如何使用C++模擬實現(xiàn)這一魔術(shù)效果,感興趣的可以了解下2024-02-02