SETVBUF(3POSIX)
๐ธ SETVBUF โ ok so you wanna control your stream's buffering ๐
setvbuf, from the POSIX Programmer's Manual. Buckle up bestie, this one's lowkey iconic.
โจ PROLOG โจ
Real talk first: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so go check the corresponding Linux manual page for the deets on Linux behavior), or honestly the interface may not be implemented on Linux at all. Not me warning you upfront ๐
๐ NAME ๐
setvbuf โ assign buffering to a stream. That's it. That's the job. Periodt.
๐ SYNOPSIS ๐
#include <stdio.h>
int setvbuf(FILE *restrict stream, char *restrict buf, int type,
size_t size);
๐ฆ DESCRIPTION ๐ฆ
Okay so the functionality described on this reference page is aligned with the ISO C standard. Any conflict between what's described here and the ISO C standard is unintentional โ no drama intended. This volume of POSIX.1โ2017 defers to the ISO C standard. Respect the elders ๐
So, the setvbuf() function may be used after the stream pointed to by stream is associated with an open file, but before any other operation (other than an unsuccessful call to setvbuf()) is performed on the stream. Timing is everything, iykyk.
The argument type determines how stream shall be buffered, as follows:
{_IOFBF}โ shall cause input/output to be fully buffered. All the buffering. Main character energy.{_IOLBF}โ shall cause input/output to be line buffered. One line at a time, cute and tidy.{_IONBF}โ shall cause input/output to be unbuffered. Raw. Unfiltered. No cap.
If buf is not a null pointer, the array it points to may be used instead of a buffer allocated by setvbuf(), and the argument size specifies the size of the array; otherwise, size may determine the size of a buffer allocated by the setvbuf() function. And heads up: the contents of the array at any time are unspecified. ๐
For info about streams, see Section 2.5, Standard I/O Streams.
โญ RETURN VALUE โญ
Upon successful completion, setvbuf() shall return 0. Otherwise, it shall return a non-zero value if an invalid value is given for type or if the request cannot be honored, and may set errno to indicate the error. It's giving "something went wrong, good luck figuring out what" ๐ญ
๐ ERRORS ๐
The setvbuf() function may fail if:
EBADFโ The file descriptor underlyingstreamis not valid.
The following sections are informative. (Yes, that's the actual disclaimer, slay.)
๐ EXAMPLES ๐
None. Yeah. Nothing. Empty. The void. ๐ซถ
๐ APPLICATION USAGE ๐
A common source of error is allocating buffer space as an ``automatic'' variable in a code block, and then failing to close the stream in the same block. Bestie. Please. Do not do this.
With setvbuf(), allocating a buffer of size bytes does not necessarily imply that all of size bytes are used for the buffer area. Rude, but true.
Applications should note that many implementations only provide line buffering on input from terminal devices. So don't come crying when your line buffering doesn't line buffer everywhere. ๐ง
๐ง RATIONALE ๐ง
None. Nothing to explain, we said what we said. ๐
๐ฎ FUTURE DIRECTIONS ๐ฎ
None. The future is unspecified. Very on brand. โจ
๐ซถ SEE ALSO ๐ซถ
- Section 2.5, Standard I/O Streams
fopen()setbuf()
The Base Definitions volume of POSIX.1โ2017, <stdio.h> ๐
๐ COPYRIGHT ๐
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
okay that's the whole page, love u, go buffer responsibly ๐
Brought to you by IEEE/The Open Group, 2017, on the SETVBUF(3POSIX) page. That's a wrap bestie ๐
๐ฆ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 ยท Other
- debian trixie: manpages-posix-dev 2017a-2 ยท Other
- ubuntu jammy: manpages-posix-dev 2017a-2 ยท Other
- ubuntu noble: manpages-posix-dev 2017a-2 ยท Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 ยท Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 ยท Other
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms