std::basic_streambuf<CharT,Traits>::pubsetbuf, std::basic_streambuf<CharT,Traits>::setbuf
来自cppreference.com
<tbody>
</tbody>
public: 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';
}
可能的输出:
356010
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 158 | C++98 | setbuf 的默认行为只有在 gptr() 不为空且不等于 egptr() 时才有指明
|
在所有场合都指明为无效果 |
参阅
[虚] |
试图以数组替换受控字符序列 ( std::basic_stringbuf<CharT,Traits,Allocator> 的虚受保护成员函数)
|
[虚] |
提供用户供应的缓冲区,或将此 filebuf 转变为无缓冲 ( std::basic_filebuf<CharT,Traits> 的虚受保护成员函数)
|
[虚] |
尝试以数组替换受控制字符序列 ( std::strstreambuf 的虚受保护成员函数)
|
| 为文件流设置缓冲区 (函数) |