| 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) |
| Types and objects | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <cstdio> | ||
void setbuf(std::FILE* stream,char* buffer); | ||
Sets the internal buffer to use for I/O operations performed on the C streamstream.
Ifbuffer is not null, equivalent tostd::setvbuf(stream, buffer,_IOFBF,BUFSIZ).
Ifbuffer is null, equivalent tostd::setvbuf(stream, nullptr,_IONBF,0), which turns off buffering.
Contents |
| stream | - | the file stream to set the buffer to |
| buffer | - | pointer to a buffer for the stream to use. If a null pointer is supplied, the buffering is turned off. If not null, must be able to hold at leastBUFSIZ characters |
(none)
IfBUFSIZ is not the appropriate buffer size,std::setvbuf can be used to change it.
std::setvbuf should also be used to detect errors, sincestd::setbuf does not indicate success or failure.
This function may only be used afterstream has been associated with an open file, but before any other operation (other than a failed call tostd::setbuf/std::setvbuf).
A common error is setting the buffer ofstdin orstdout to an array whose lifetime ends before the program terminates:
int main(){char buf[BUFSIZ]; std::setbuf(stdin, buf);}// lifetime of buf ends, undefined behavior
std::setbuf may be used to disable buffering on streams that require immediate output.
#include <chrono>#include <cstdio>#include <thread> int main(){usingnamespace std::chrono_literals; std::setbuf(stdout, nullptr);// unbuffered stdoutstd::putchar('a');// appears immediately on unbuffered streamstd::this_thread::sleep_for(1s);std::putchar('b');}
Output:
ab
| sets the buffer and its size for a file stream (function)[edit] | |
C documentation forsetbuf | |