std::basic_streambuf<CharT,Traits>::underflow
来自 cppreference.cn
< cpp | io | basic streambuf
protected: virtual int_type underflow(); |
||
通过更新指向输入区域的指针(如果需要)并从输入序列中读取更多数据(如果适用),确保输入区域中至少有一个字符可用。成功时返回该字符的值(使用 Traits::to_int_type(c)) 转换为 int_type
),失败时返回 Traits::eof()。
该函数可能会更新 gptr
、egptr
和 eback
指针,以定义新加载数据的位置(如果有)。失败时,该函数确保 gptr() == nullptr 或 gptr() == egptr。
该函数的基类版本不执行任何操作。派生类可以重写此函数,以便在耗尽的情况下允许更新读取区域。
目录 |
[edit] 参数
(无)
[edit] 返回值
成功调用后,get 指针指向的字符的值;否则为 Traits::eof()。
该函数的基类版本返回 traits::eof()。
[edit] 注
仅当 gptr() == nullptr 或 gptr() >= egptr() 时,std::streambuf 的公共函数才会调用此函数。
[edit] 示例
运行此代码
#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
[edit] 参见
[虚函数] |
从关联的输入序列读取字符到读取区域,并前进下一个指针 (虚函数 保护成员函数) |
[虚函数] |
将字符从写入区域写入到关联的输出序列 (虚函数 保护成员函数) |
[虚函数] |
从关联的文件读取 (std::basic_filebuf<CharT,Traits> 的虚函数 保护成员函数) |
[虚函数] |
返回输入序列中可用的下一个字符 (std::basic_stringbuf<CharT,Traits,Allocator> 的虚函数 保护成员函数) |
[虚函数] |
从输入序列中读取一个字符,而不前进下一个指针 (std::strstreambuf 的虚函数 保护成员函数) |