I/O manipulators | ||||
Print functions(C++23) | ||||
C-style I/O | ||||
Buffers | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++20) | ||||
Streams | ||||
Abstractions | ||||
File I/O | ||||
String I/O | ||||
Array I/O | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
Synchronized Output | ||||
(C++20) | ||||
Types | ||||
Error category interface | ||||
(C++11) | ||||
(C++11) |
Public member functions | ||||
(C++11) | ||||
(C++11) | ||||
(C++20) | ||||
Protected member functions | ||||
basic_stringbuf::underflow | ||||
Non-member functions | ||||
(C++11) | ||||
Exposition-only member functions | ||||
protected: virtual int_type underflow() | ||
Reads the next character from the get area of the buffer.
Specifically:
Any character in the buffer which has been initialized, regardless of whether it originated from the string passed in the constructor or was appended byoverflow(), is considered to be part of the input sequence.
Contents |
(none)
Traits::to_int_type(*gptr()) (the next character to read in the get area) in case of success, orTraits::eof() in case of failure.
#include <iostream>#include <sstream> struct mybuf:std::stringbuf{ mybuf(conststd::string& new_str,std::ios_base::openmode which=std::ios_base::in|std::ios_base::out):std::stringbuf(new_str, which){} int_type overflow(int_type c){std::cout<<"Before overflow(): get area size is "<< egptr()- eback()<<", the put area size is "<< epptr()- pbase()<<'\n'; int_type rc= std::stringbuf::overflow(c);std::cout<<"After overflow(): get area size is "<< egptr()- eback()<<", put area size is "<< epptr()- pbase()<<'\n';return rc;} int_type underflow(){std::cout<<"Before underflow(): get area size is "<< egptr()- eback()<<", put area size is "<< epptr()- pbase()<<'\n'; int_type ch= std::stringbuf::underflow();std::cout<<"After underflow(): get area size is "<< egptr()- eback()<<", put area size is "<< epptr()- pbase()<<'\n'; if(ch==EOF)std::cout<<"underflow() returns EOF\n";elsestd::cout<<"underflow() returns '"<<char(ch)<<"'\n";return ch;}}; int main(){ mybuf sbuf("123");// read-write streamstd::iostream stream(&sbuf);int n; stream>> n;// calls sgetc() four times// three calls return the characters '1', '2', '3'// the fourth call, gptr() == egptr() and underflow() is called// underflow returns EOFstd::cout<<"n = "<< n<<'\n'; stream.clear();// clear the eofbit stream<<"123456";// sputc() is called 6 times// first three calls store "123" in the existing buffer// 4th call finds that pptr() == epptr() and calls overflow()// overflow() grows the buffer and sets egptr() to 4// 5th and 6th calls store '5' and '6', advancing pptr() stream>> n;// calls sgetc() 4 times// 1st call returns the '4' that was made available by overflow()// on the 2nd call, egptr() == egptr() and underflow() is called// underflow advances egptr() to equal pptr() (which is 6)// 3rd sgetc() returns '6'// 4th sgetc() finds gptr() == egptr(), calls underflow()// underflow() returns EOF std::cout<<"n = "<< n<<'\n';}
Possible output:
Before underflow(): get area size is 3, put area size is 15After underflow(): get area size is 3, put area size is 15underflow() returns EOFn = 123Before underflow(): get area size is 3, put area size is 15After underflow(): get area size is 6, put area size is 15underflow() returns '4'Before underflow(): get area size is 6, put area size is 15After underflow(): get area size is 6, put area size is 15underflow() returns EOFn = 456
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 432 | C++98 | it was unclear whether characters appended byoverflow() are considered to be part of the input sequence | made clear |
[virtual] | reads characters from the associated input sequence to the get area (virtual protected member function of std::basic_streambuf<CharT,Traits> )[edit] |
[virtual] | reads from the associated file (virtual protected member function of std::basic_filebuf<CharT,Traits> )[edit] |
[virtual] | reads a character from the input sequence without advancing the next pointer (virtual protected member function of std::strstreambuf )[edit] |
reads one character from the input sequence without advancing the sequence (public member function of std::basic_streambuf<CharT,Traits> )[edit] |