名稱空間
變體
操作

std::basic_streambuf<CharT,Traits>::underflow

來自 cppreference.com
< cpp‎ | io‎ | basic streambuf
 
 
 
 
protected:
virtual int_type underflow();

透過(若需要)更新輸入區的指標和從輸入序列(若可用)讀入更多資料,來確保輸入區中至少有一個可用字元。成功時,返回該字元的值(以 Traits::to_int_type(c) 轉換到 int_type);失敗時,返回 Traits::eof()

此函式可更新 gptregptreback 指標,以定義新載入資料(若存在)的位置。失敗時,函式確保 gptr() == nullptrgptr() == egptr

基類版本的此函式不進行任何操作。派生類可重寫此函式,以允許在獲取區耗盡時更新它。

目錄

[編輯] 引數

(無)

[編輯] 返回值

成功時為呼叫後獲取指標所指向的字元的值,否則為 Traits::eof()

基類版本的此函式返回 traits::eof()

[編輯] 注意

std::streambuf 的公開函式僅在 gptr() == nullptrgptr() >= egptr() 時呼叫此函式。

[編輯] 示例

#include <iostream>
#include <sstream>
 
class null_filter_buf : public std::streambuf
{
    std::streambuf* src;
    char ch; // single-byte buffer
protected:
    int underflow()
    {
        traits_type::int_type i;
        while ((i = src->sbumpc()) == '\0')
            ; // skip zeroes
        if (!traits_type::eq_int_type(i, traits_type::eof()))
        {
            ch = traits_type::to_char_type(i);
            setg(&ch, &ch, &ch+1); // make one read position available
        }
        return i;
    }
public:
    null_filter_buf(std::streambuf* buf) : src(buf)
    {
        setg(&ch, &ch + 1, &ch + 1); // buffer is initially full
    }
};
 
void filtered_read(std::istream& in)
{
    std::streambuf* orig = in.rdbuf();
    null_filter_buf buf(orig);
    in.rdbuf(&buf);
    for (char c; in.get(c);)
        std::cout << c;
    in.rdbuf(orig);
}
 
int main()
{
    char a[] = "This i\0s \0an e\0\0\0xample";
    std::istringstream in(std::string(std::begin(a), std::end(a)));
    filtered_read(in);
}

輸出

This is an example

[編輯] 參閱

[虛]
從關聯輸入序列中讀取字元到獲取區並推進下一個指標
(虛保護成員函式) [編輯]
從放置區向關聯輸出序列寫入字元
(虛保護成員函式) [編輯]
從關聯檔案讀取
(std::basic_filebuf<CharT,Traits> 的虛保護成員函式) [編輯]
返回輸入序列中下一個可用的字元
(std::basic_stringbuf<CharT,Traits,Allocator> 的虛保護成員函式) [編輯]
從輸入序列中讀取一個字元,但不推進下一個指標
(std::strstreambuf 的虛保護成員函式) [編輯]