std::basic_streambuf<CharT,Traits>::pubsetbuf, std::basic_streambuf<CharT,Traits>::setbuf
来自cppreference.com
                    
                                        
                    < cpp | io | basic streambuf
                    
                                                            
                    |   basic_streambuf<CharT, Traits>* pubsetbuf( char_type* s, std::streamsize n )  | 
(1) | |
|   protected: virtual basic_streambuf<CharT, Traits>* setbuf( char_type* s, std::streamsize n )  | 
(2) | |
1) 调用最终派生类上的 
setbuf(s, n)2) 此函数的基类版本无效果。派生类可覆写此函数,以允许移除或替换受控制字符序列(缓冲区)为用户提供的数组,或为任何实现特定的目的。
参数
| s | - | 指向用户提供的缓冲区中首个 CharT 的指针 | 
| n | - | 用户提供缓冲区中的 CharT 元素数 | 
返回值
1) 
setbuf(s, n) 的返回值2) this
示例
为读取提供 10k 缓冲区。 Linux 上,可用 strace 工具观察实际读取的字节数
运行此代码
#include <fstream> #include <iostream> #include <string> int main() { int cnt = 0; std::ifstream file; char buf[1024*10 + 1]; file.rdbuf()->pubsetbuf(buf, sizeof buf); file.open("/usr/share/dict/words"); for (std::string line; getline(file, line);) { cnt++; } std::cout << cnt << '\n'; }
 
参阅
|    [虚]  | 
  试图以数组替换受控字符序列  ( std::basic_stringbuf<CharT,Traits,Allocator> 的虚受保护成员函数)  | 
|    [虚]  | 
  提供用户供应的缓冲区,或将此 filebuf 转变为无缓冲  ( std::basic_filebuf<CharT,Traits> 的虚受保护成员函数)  | 
|    [虚]  | 
  试图以数组替换受控制字符序列  ( std::strstreambuf 的虚受保护成员函数)  | 
|   为文件流设置缓冲区  (函数)  |