首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::basic_filebuf::setbuf

protected: virtual std::basic_streambuf<CharT, Traits>* setbuf( char_type* s, std::streamsize n )

?

?

如果s为空指针,并且n为零,文件就变成了无缓冲用于输出,意义pbase()pptr()都是空的,任何输出都会立即发送到文件中。

否则,打电话到setbuf()用用户提供的字符数组替换内部缓冲区%28(受控字符序列%29),该字符数组的第一个元素指向s并允许std::basic_filebuf对象以最多可使用的方式使用。n用于缓冲的数组中的字节。

此函数是受保护的虚拟函数,只能通过pubsetbuf()派生的用户定义类的成员函数。std::basic_filebuf...

参数

s

-

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...

注记

可以使用此函数的条件和使用提供的缓冲区的方式是实现定义的。

  • gcc 4.6 libstdc++setbuf()只有在std::basic_filebuf不与文件%28关联,否则%29无效。使用用户提供的缓冲区,从文件读取。n-1一次字节数。
  • clang++3.0 libc++

setbuf()打开文件后可能会调用,但在任何I/O%28 5月崩溃之前,否则将调用%29。使用用户提供的缓冲区,从文件中读取的最大倍数为4096倍,符合缓冲区的要求。

  • VisualStudio 2010

setbuf()可以在任何时候调用,甚至在发生一些I/O之后。缓冲区的当前内容(如果有的话)丢失。

该标准不为此函数定义任何行为,但setbuf(0, 0)需要在发生任何I/O之前调用以设置未缓冲的输出。

提供10K的阅读缓冲区。在Linux上,可以使用strace实用程序来观察实际读取的字节数。

二次

代码语言:javascript
复制
#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';
}

二次

另见

pubsetbuf

invokes setbuf() (public member function of std::basic_streambuf)

setvbuf

sets the buffer and its size for a file stream (function)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com