std::basic_streambuf<CharT,Traits>:: underflow
|
protected
:
virtual int_type underflow ( ) ; |
||
通过更新输入区域指针(如有需要)并从输入序列读取更多数据(如适用),确保输入区域中至少有一个字符可用。成功时返回该字符的值(转换为
int_type
,即
Traits
::
to_int_type
(
c
)
),失败时返回
Traits
::
eof
(
)
。
该函数可能更新
gptr
、
egptr
和
eback
指针以定义新加载数据的位置(如果有)。若执行失败,该函数确保
gptr
(
)
==
nullptr
或
gptr
(
)
==
egptr
成立。
基类版本的函数不执行任何操作。派生类可以重写此函数,以便在耗尽情况下允许更新获取区域。
目录 |
参数
(无)
返回值
成功调用后 获取指针 所指向字符的值,否则返回 Traits :: eof ( ) 。
该函数的基类版本返回 traits :: eof ( ) 。
注释
std::streambuf 的公共函数仅在 gptr ( ) == nullptr 或 gptr ( ) >= egptr ( ) 时调用此函数。
示例
#include <iostream> #include <sstream> class null_filter_buf : public std::streambuf { std::streambuf* src; char ch; // 单字节缓冲区 protected: int underflow() { traits_type::int_type i; while ((i = src->sbumpc()) == '\0') ; // 跳过零值 if (!traits_type::eq_int_type(i, traits_type::eof())) { ch = traits_type::to_char_type(i); setg(&ch, &ch, &ch+1); // 提供一个读取位置 } return i; } public: null_filter_buf(std::streambuf* buf) : src(buf) { setg(&ch, &ch + 1, &ch + 1); // 缓冲区初始为满状态 } }; 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
参见
|
[virtual]
|
从关联的输入序列读取字符到获取区并推进下一个指针
(虚受保护成员函数) |
|
[virtual]
|
从放置区向关联的输出序列写入字符
(虚受保护成员函数) |
|
[virtual]
|
从关联文件读取
(
std::basic_filebuf<CharT,Traits>
的虚受保护成员函数)
|
|
[virtual]
|
返回输入序列中可用的下一个字符
(
std::basic_stringbuf<CharT,Traits,Allocator>
的虚受保护成员函数)
|
|
[virtual]
|
从输入序列读取字符但不推进下一个指针
(
std::strstreambuf
的虚受保护成员函数)
|