std::basic_streambuf<CharT,Traits>::pubsetbuf, std::basic_streambuf<CharT,Traits>::setbuf
From cppreference.com
< cpp | io | basic streambuf
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Input/output library
Print functions (C++23)
Buffers
(C++23)
(C++98/26*)
(C++20)
Streams
Abstractions
File I/O
String I/O
Array I/O
(C++23)
(C++23)
(C++23)
(C++98/26*)
(C++98/26*)
(C++98/26*)
Synchronized Output
(C++20)
Types
Error category interface
(C++11)
(C++11)
std::basic_streambuf
Public member functions
Locales
Positioning
basic_streambuf::pubsetbuf
Get area
Put area
Putback
Protected member functions
(C++11)
(C++11)
Locales
Positioning
basic_streambuf::setbuf
Get area
Put area
Putback
public:
basic_streambuf<CharT, Traits>* pubsetbuf( char_type* s, std::streamsize n )
(1)
basic_streambuf<CharT, Traits>* pubsetbuf( char_type* s, std::streamsize n )
protected:
virtual basic_streambuf<CharT, Traits>* setbuf( char_type* s, std::streamsize n )
(2)
virtual basic_streambuf<CharT, Traits>* setbuf( char_type* s, std::streamsize n )
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
CharT
in the user-provided buffer
n
-
the number of
CharT
elements in the user-provided buffer
[edit] Return value
1) The return value of setbuf(s, n).
2) this
[edit] Example
Provides 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[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'; }
Possible output:
356010
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 158 | C++98 | the default behavior of setbuf was only specifiedif gptr() is not null and not equal to egptr() |
specified as no-op for all cases |
[edit] See also
[virtual]
(virtual protected member function of
std::basic_stringbuf<CharT,Traits,Allocator>
) [edit]
[virtual]
(virtual protected member function of
std::basic_filebuf<CharT,Traits>
) [edit]
[virtual]
(virtual protected member function of
std::strstreambuf
) [edit]