欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++中std::ifstream的使用方法介紹

 更新時間:2024年06月13日 09:54:02   作者:賽先生.AI  
這篇文章主要給大家介紹了關(guān)于C++中std::ifstream使用方法的相關(guān)資料,std::ifstream 是輸入文件流類,用于從文件中讀取數(shù)據(jù),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下

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用實(shí)際的例程介紹其使用方法。

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(); // 獲取實(shí)際讀取的字節(jié)數(shù)
        /// 處理讀取到的數(shù)據(jù)...
        /// 這里只是簡單地打印出實(shí)際讀取的字節(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 的異同點(diǎn)

std::ifstream 和 std::ofstream 都是C++標(biāo)準(zhǔn)庫中的文件流類,用于進(jìn)行文件的輸入和輸出操作。

異同點(diǎn)如下:

相同點(diǎn):

都是用于文件操作的流類。

都需要通過文件路徑來創(chuàng)建文件流對象。

都可以使用相同的成員函數(shù)來進(jìn)行文件的讀寫操作。

不同點(diǎ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 對象可以使用 >> 運(yùn)算符來從文件中讀取數(shù)據(jù)。

std::ofstream 對象可以使用 << 運(yùn)算符來將數(shù)據(jù)寫入文件。

總結(jié) 

到此這篇關(guān)于C++中std::ifstream使用方法的文章就介紹到這了,更多相關(guān)C++ std::ifstream使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論