名稱空間
變體
操作

std::basic_ios<CharT,Traits>::rdbuf

來自 cppreference.com
< cpp‎ | io‎ | basic ios
 
 
 
 
std::basic_streambuf<CharT, Traits>* rdbuf() const;
(1)
std::basic_streambuf<CharT, Traits>* rdbuf( std::basic_streambuf<CharT, Traits>* sb );
(2)

管理關聯的流緩衝區。

1) 返回關聯的流緩衝區。如果沒有關聯的流緩衝區,返回空指標。
2) 將關聯的流緩衝區設定為 sb。透過呼叫 clear() 清除錯誤狀態。返回操作前關聯的流緩衝區。如果沒有關聯的流緩衝區,返回空指標。

目錄

[編輯] 引數

sb - 要關聯的流緩衝區。

[編輯] 返回值

關聯的流緩衝區,如果沒有關聯的流緩衝區則返回空指標。

[編輯] 異常

可能丟擲實現定義的異常。

[編輯] 示例

#include <iostream>
#include <sstream>
 
int main()
{
    std::ostringstream local;
    auto cout_buff = std::cout.rdbuf(); // save pointer to std::cout buffer
 
    std::cout.rdbuf(local.rdbuf()); // substitute internal std::cout buffer with
                                    // buffer of 'local' object
 
    // now std::cout work with 'local' buffer
    // you don't see this message
    std::cout << "some message";
 
    // go back to old buffer
    std::cout.rdbuf(cout_buff);
 
    // you will see this message
    std::cout << "back to default buffer\n";
 
    // print 'local' content
    std::cout << "local content: " << local.str() << "\n";
}

輸出

back to default buffer
local content: some message

[編輯] 參閱

替換 rdbuf 而不清除其錯誤狀態
(保護成員函式) [編輯]