std::basic_filebuf::setbuf(3)
std::basic_filebuf::setbuf(3)
okay so bestie, this page comes from the C++ Standard Libary (yep, that's the spelling in the header β iconic typo energy, no cap π). It's all about std::basic_filebuf::setbuf, and it's giving protected-virtual-buffer makeover energy β highkey iconic. Let's go β¨π
πΈ NAME πΈ
std::basic_filebuf::setbuf - std::basic_filebuf::setbuf
That's it. That's the name. We move. π«Ά
π¦ Synopsis π¦
protected:
virtual std::basic_streambuf<CharT, Traits>* setbuf( char_type* s,
std::streamsize
n )
So here's the tea: 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. periodt π
Otherwise, a call to setbuf() replaces the internal buffer (the controlled character sequence) with the user-supplied character array whose first element is pointed to by s, and it allows this std::basic_filebuf object to use up to n bytes in that array for buffering. slay β¨
This function is protected virtual, so it may only be called through pubsetbuf() or from member functions of a user-defined class derived from std::basic_filebuf. iykyk π
π Parameters π
sβ pointer to the firstCharTin the user-provided buffer or nullnβ the number ofCharTelements in the user-provided buffer or zero
π Return value π
this
literally just this β obsessed with the main character energy. π
π₯ Notes π₯
Vibe check: the conditions when this function may be used and the way in which the provided buffer is used is implementation-defined. So yes, bestie, your implementation gets a vote. π³οΈ
- GCC 4.6 libstdc++ β
setbuf()may only be called when thestd::basic_filebufis not associated with a file (has no effect otherwise). With a user-provided buffer, reading from file readsn-1bytes at a time. - Clang++3.0 libc++ β
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 of4096that fit in the buffer.
β οΈ "may crash otherwise" is not a drill, bestie. Do not test it for fun. π
- Visual Studio 2010 β
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 that setbuf(0, 0) called before any I/O has taken place is required to set unbuffered output. That's the one guaranteed lil' thing. periodt π
πΈ Example πΈ
Provides a 10k buffer for reading. On linux, the strace utility may be used to observe the actual number of bytes read. nerd sniping, but make it cute π
// 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';
}
π Possible output π
356010
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. retroactive lore drop, honestly. π
- DR:
LWG 173- Applied to:
C++98 - Behavior as published: the type of
nwas misspecified asint - Correct behavior: corrected to
std::streamsize
- Applied to:
π See also π
pubsetbufβ invokessetbuf()(public member function ofstd::basic_streambuf<CharT,Traits>)setvbufβ sets the buffer and its size for a file stream (function)
mwah π β source/version/date: http://cppreference.com, 2024.06.10, std::basic_filebuf::setbuf(3). Byeee π«Ά fr fr
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms