std::basic_filebuf<CharT,Traits>::setbuf

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.

# Declarations

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

# Parameters

# Return value

this

# Notes

The conditions when this function may be used and the way in which the provided buffer is used is implementation-defined.

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.

# Example

#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';
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 173C++98the type of n was misspecified as intcorrected to std::streamsize

# See also