std::setbuf

Header: <cstdio>

Sets the internal buffer to use for I/O operations performed on the C stream stream.

# Declarations

void setbuf( std::FILE* stream, char* buffer );

# Parameters

# Return value

(none)

# Notes

If BUFSIZ is not the appropriate buffer size, std::setvbuf can be used to change it.

std::setvbuf should also be used to detect errors, since std::setbuf does not indicate success or failure.

This function may only be used after stream has been associated with an open file, but before any other operation (other than a failed call to std::setbuf/std::setvbuf).

A common error is setting the buffer of stdin or stdout to an array whose lifetime ends before the program terminates:

# Example

#include <chrono>
#include <cstdio>
#include <thread>
 
int main()
{
    using namespace std::chrono_literals;
 
    std::setbuf(stdout, nullptr); // unbuffered stdout
    std::putchar('a'); // appears immediately on unbuffered stream
    std::this_thread::sleep_for(1s);
    std::putchar('b');
}

# See also