Namespaces
Variants
Views
Actions

std::wbuffer_convert::rdbuf

From cppreference.com
< cpp‎ | locale‎ | wbuffer convert
Revision as of 18:35, 2 November 2012 by P12bot (Talk | contribs)

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <locale>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td >
std::streambuf* rdbuf() const;
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
std::streambuf* rdbuf( std::streambuf* bytebuf );
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

1) returns the pointer to the underlying byte stream

2) replaces the associated byte stream with bytebuf .

Return value

1) the current underlying byte stream

2) the previous byte stream

Example

#include <iostream>
#include <sstream>
#include <locale>
#include <codecvt>
int main()
{
    // convert UTF-8 to UCS4
    std::stringbuf utf8buf(u8"z\u00df\u6c34\U0001d10b");  // or u8"zß水𝄋"
                       // or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b";
    std::wbuffer_convert<std::codecvt_utf8<wchar_t>> conv(&utf8buf);
    std::wistream ucsbuf(&conv);
    std::cout << "Reading from a UTF-8 stringbuf via wbuffer_convert:\n";
    for(wchar_t c; ucsbuf.get(c); )
        std::cout << std::hex << std::showbase << c << '\n';
 
    // reuse the same wbuffer_convert to handle UCS4 to UTF-8 output
    conv.rdbuf(std::cout.rdbuf());
    std::wostream out(&conv);
    std::cout << "Sending UCS4 data to std::cout via wbuffer_convert:\n";
    out << L"z\u00df\u6c34\U0001d10b\n";
}

Output:

Reading from a UTF-8 stringbuf via wbuffer_convert:
0x7a
0xdf
0x6c34
0x1d10b
Sending UCS4 data to std::cout via wbuffer_convert:
zß水𝄋

See also

Template:cpp/locale/wbuffer convert/dcl list wbuffer convert