std::setvbuf(3)
π std::setvbuf(3) π
okay bestie, this page is from the C++ Standard Libary manual (yes, that spelling is in the header, we move π), and the main character is std::setvbuf β she changes how a file stream does its buffering. It's giving low-level stdio realness, no cap β¨
πΈ Synopsis πΈ
Defined in header <cstdio>:
int setvbuf( std::FILE* stream, char* buffer, int mode, std::size_t size );
So std::setvbuf changes the buffering mode of the file stream stream to whatever the argument mode says. In addition, here's the extra lore π
- If
bufferis a null pointer, it resizes the internal buffer tosize. - If
bufferis not a null pointer, it tells the stream to use the user-provided buffer of sizesizebeginning atbuffer. The stream must be closed (withstd::fclose) before the lifetime of the array pointed to bybufferends. The contents of the array after a successful call tostd::setvbufare indeterminate, and any attempt to use it is undefined behavior β like, absolutely do not, bestie π«Ά
π Parameters π
streamβ the file stream to set the buffer to.bufferβ pointer to a buffer for the stream to use, or a null pointer to changesizeandmodeonly.modeβ the buffering mode to use. It can be one of these values:_IOFBFβ full buffering._IOLBFβ line buffering._IONBFβ no buffering.
sizeβ size of the buffer.
π₯ Return value π₯
0 on success or nonzero on failure. That's it, that's the whole vibe check π
π«Ά Notes π«Ά
- This function may only be used after
streamhas been associated with an open file, but before any other operation (other than a failed call tostd::setbuf/std::setvbuf). - Not all
sizebytes will necessarily be used for buffering: the actual buffer size is usually rounded down to a multiple of2, a multiple of page size, etc. - On many implementations, line buffering is only available for terminal input streams.
- A common error is setting the buffer of
stdinorstdoutto an array whose lifetime ends before the program terminates:
int main()
{
char buf[BUFSIZ];
std::setbuf(stdin, buf);
} // lifetime of buf ends, undefined behavior
- The default buffer size
BUFSIZis expected to be the most efficient buffer size for file I/O on the implementation, but POSIXfstatoften provides a better estimate. iykyk β
π¦ Example π¦
One use case for changing buffer size is when a better size is known. Okay, // Run this code era:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sys/stat.h>
int main()
{
std::FILE* fp = std::fopen("/tmp/test.txt", "w+");
if (!fp)
{
std::perror("fopen");
return EXIT_FAILURE;
}
struct stat stats;
if (fstat(fileno(fp), &stats) == -1) // POSIX only
{
std::perror("fstat");
return EXIT_FAILURE;
}
std::cout << "BUFSIZ is " << BUFSIZ << ", but optimal block size is "
<< stats.st_blksize << '\n';
if (std::setvbuf(fp, nullptr, _IOFBF, stats.st_blksize) != 0)
{
std::perror("setvbuf failed"); // POSIX version sets errno
return EXIT_FAILURE;
}
// Read entire file: use truss/strace to observe the read(2) syscalls used
for (int ch; (ch = std::fgetc(fp)) != EOF;)
{}
std::fclose(fp);
return EXIT_SUCCESS;
}
β Possible output β
BUFSIZ is 8192, but optimal block size is 65536
π See also π
setbufβ sets the buffer for a file stream (function).setbufβ provides user-supplied buffer or turns this filebuf unbuffered [virtual] (virtual protected member function ofstd::basic_filebuf<CharT,Traits>).- C documentation for
setvbufβ the C sibling, if you're feeling multilingual π
mwah π this page is from http://cppreference.com, dated 2024.06.10, and it's literally page std::setvbuf(3) β slay.
π¦ 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