std::basic_filebuf::setbuf
来自cppreference.com
< cpp | io | basic filebuf
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
protected: virtual std::basic_streambuf<CharT, Traits>* setbuf( char_type* s, std::streamsize n ) |
||
s
是一个空指针,n
是零,的filebuf成为“无缓冲”的输出,意思是pbase()
和pptr()
是空的,会被立即发送任何输出到文件原文:
If
s
is a null pointer and n
is zero, the filebuf becomes unbuffered for output, meaning pbase()
and pptr()
are null and any output is immediately sent to file.否则,调用
setbuf()
取代了内部缓冲区(控制字符序列)用户提供的字符数组,该数组的第一个元素是指向s
,并允许这std::basic_filebuf对象使用该数组中的n
字节的缓冲原文:
Otherwise, a call to
setbuf()
replaces the internal buffer (the controlled character sequence) with the user-supplied character array whose first element is pointed to by s
and allows this std::basic_filebuf object to use up to n
bytes in that array for buffering.此功能是受保护的虚拟,它可能只被称为通过
pubsetbuf()
或来自std::basic_filebuf
一个用户定义的类的成员函数.原文:
This function is protected virtual, it may only be called through
pubsetbuf()
or from member functions of a user-defined class derived from std::basic_filebuf
.目录 |
[编辑] 参数
s | - | 指针的第一个字节的用户提供的缓冲区,则返回null
原文: pointer to the first byte in the user-provided buffer or null |
n | - | 中的字节数的用户提供缓冲区或零
原文: the number of bytes in the user-provided buffer or zero |
[编辑] 返回值
*this,转换为基类
std::basic_streambuf
.原文:
*this, cast to the base class
std::basic_streambuf
.[编辑] 注释
的条件时可使用此功能和在其中所提供的缓冲区被使用的方式是实现定义的.
原文:
The conditions when this function may be used and the way in which the provided buffer is used is implementation-defined.
- GCC 4.6的libstdc + +
-
setbuf()
可能只被称为时的std::basic_filebuf是不相关的文件,有没有效果,否则,使用用户提供的缓冲区,读从文件读取n-1
字节,在时间原文:setbuf()
may only be called when the std::basic_filebuf is not associated with a file (has no effect otherwise). With a user-provided buffer, reading from file readsn-1
bytes at a time.
- 铛+ +3.0的libc + +
-
setbuf()
可被称为后打开文件,但在任何I / O(否则可能会崩溃)用户提供的缓冲区。从文件中读取,读取最大为4096的倍数,适合在缓冲区中原文:setbuf()
may be called after opening the file, but before any I/O (may crash otherwise). With a user-provided buffer, reading from file reads largest multiples of 4096 that fit in the buffer.
- Visual Studio 2010中
-
setbuf()
可能被要求在任何时候,即使在一些I / O的发生。当前缓冲区的内容,如果有的话,会丢失原文:setbuf()
may be called at any time, even after some I/O took place. Current contents of the buffer, if any, are lost.
本标准不定义此功能除了
setbuf(0, 0)
所谓的任何I / O之前已经发生,需要设置缓冲输出的任何行为原文:
The standard does not define any behavior for this function except that
setbuf(0, 0)
called before any I/O has taken place is required to set unbuffered output.[编辑] 示例
提供一个10K的缓冲区的读取。在Linux上,与strace实用程序可用于观察实际读取的字节数
原文:
provide a 10k buffer for reading. On linux, the strace utility may be used to observe the actual number of bytes read
#include <fstream> #include <iostream> #include <string> int main() { int cnt=0; std::ifstream file; char buf[10241]; file.rdbuf()->pubsetbuf(buf, sizeof buf); file.open("/usr/share/dict/words"); for(std::string line; getline(file, line); ) cnt++; std::cout << cnt << '\n'; }
[编辑] 另请参阅
调用setbuf() (公共成员函数of std::basic_streambuf )
| |
设置文件流的缓冲区,其大小 原文: sets the buffer and its size for a file stream (函数) |