I/O manipulators | ||||
Print functions(C++23) | ||||
C-style I/O | ||||
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) |
Public member functions | ||||
(C++11) | ||||
(C++11) | ||||
(C++26) | ||||
Protected member functions | ||||
basic_filebuf::setbuf | ||||
Non-member functions | ||||
(C++11) |
protected: virtualstd::basic_streambuf<CharT, Traits>* setbuf( char_type* s,std::streamsize n) | ||
Ifs is a null pointer andn is zero, the filebuf becomesunbuffered for output, meaningpbase()
andpptr()
are null and any output is immediately sent to file.
Otherwise, a call tosetbuf()
replaces the internal buffer (the controlled character sequence) with the user-supplied character array whose first element is pointed to bys and allows thisstd::basic_filebuf object to use up ton bytes in that array for buffering.
This function is protected virtual, it may only be called throughpubsetbuf()
or from member functions of a user-defined class derived fromstd::basic_filebuf
.
Contents |
s | - | pointer to the firstCharT in the user-provided buffer or null |
n | - | the number ofCharT elements in the user-provided buffer or zero |
this
The conditions when this function may be used and the way in which the provided buffer is used is implementation-defined.
setbuf()
may only be called when thestd::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.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.setbuf()
may be called at any time, even after some I/O took place. Current contents of the buffer, if any, are lost.The standard does not define any behavior for this function except thatsetbuf(0,0) called before any I/O has taken place is required to set unbuffered output.
Provides 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';}
Possible output:
356010
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 173 | C++98 | the type ofn was misspecified asint | corrected tostd::streamsize |
invokessetbuf() (public member function of std::basic_streambuf<CharT,Traits> )[edit] | |
sets the buffer and its size for a file stream (function)[edit] |