std::basic_streambuf::pubsetbuf, std::basic_streambuf::setbuf - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::basic_streambuf::pubsetbuf, std::basic_streambuf::setbuf

From 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) Calls `setbuf(s, n)` of the most derived class
2) The base class version of this function has no effect. The derived classes may override this function to allow removal or replacement of the controlled character sequence (the buffer) with a user-provided array, or for any other implementation-specific purpose.

### [edit] Parameters

s | - | pointer to the first byte in the user-provided buffer
n | - | the number of bytes in the user-provided buffer

### [edit] Return value

1) The return value of `setbuf(s, n)`
2) this

### [edit] Example

provide a 10k buffer for reading. On linux, the strace utility may be used to observe the actual number of bytes read

Run this code

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

### [edit] See also

setbuf [virtual] | attempts to replace the controlled character sequence with an array (virtual protected member function of `std::basic_stringbuf`) [edit]
setbuf [virtual] | provides user-supplied buffer or turns this filebuf unbuffered (virtual protected member function of `std::basic_filebuf`) [edit]
setbuf [virtual] | attempts to replace the controlled character sequence with an array (virtual protected member function of `std::strstreambuf`) [edit]
setbuf | sets the buffer for a file stream (function) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_streambuf/pubsetbuf&oldid=69876"