The C++ std::ios::rdbuf() function is used to get or set the underlying stream buffer associated with an input/outpyt stream. When this function is invoked, it returns a pointer to the stream buffer for the stream, allowing low-level manipulation of the streams buffer.
SyntaxFollowing is the syntax for std::ios::rdbuf() function.
streambuf* rdbuf() const; streambuf* rdbuf (streambuf* sb);Parameters
This function returns a pointer to the stram buffer objcet associated with the stream before the call.
ExceptionsIf an exception is thrown, the stream is in a valid state. It throws an exception of member type failure if sb is a null pointer and member exceptions was set to throw for badbit.
Data racesAccesses or modifies the stream object.
Concurrent access to the same stream object may cause data races.
ExampleLet's look at the following example, where we are going to swap buffers between two streams.
#include <iostream> #include <sstream> int main() { std::stringstream a, b; a << "TutorialsPoint"; b << "Welcome"; std::swap(*a.rdbuf(), *b.rdbuf()); std::cout << "a Now Contains: " << a.str() << std::endl; std::cout << "b Now Contains: " << b.str() << std::endl; return 0; }Output
Output of the above code is as follows −
a Now Contains: Welcome b Now Contains: TutorialsPointExample
Consider the following example, where we are going to redirect cin to read from std::istringstream object.
#include <iostream> #include <sstream> int main() { std::istringstream a("12 2 334"); int x,y,z; std::cin.rdbuf(a.rdbuf()); std::cin >> x >> y >> z; std::cout << "Result : " << x << " " << y << " " << z << std::endl; std::cin.rdbuf(std::cin.rdbuf()); return 0; }Output
Following is the output of the above code −
Result : 12 2 334
ios.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4