C++ <cstdio> - _IOFBF, _IOLBF, _IONBF
The C++ <cstdio> _IOFBF, _IOLBF, _IONBF macro constants are used as argument to setvbuf() function indicating fully buffered I/O, line buffered I/O and unbuffered I/O respectively.
Macros | Description |
---|---|
_IOFBF | Full buffering - On output, data is written once the buffer is full (or flushed). On Input, the buffer is filled when an input operation is requested and the buffer is empty. |
_IOLBF | Line buffering - On output, data is written when a newline character is inserted into the stream or when the buffer is full (or flushed), whatever happens first. On Input, the buffer is filled up to the next newline character when an input operation is requested and the buffer is empty. |
_IONBF | No buffering - No buffer is used. Each I/O operation is written as soon as possible. In this case, the buffer and size parameters are ignored. |
Example:
The example below describes the usage of setvbuf() function.
#include <cstdio> #include <cstdlib> int main() { char buf[50]; if(setvbuf(stdout, buf, _IOFBF, sizeof buf)) { perror("Failed to change the buffer of stdout"); return EXIT_FAILURE; } printf("Programming"); //The buffer contains "Programming" //but nothing is written to stdout yet fflush(stdout); //Now "Programming" is written to stdout if(setvbuf(stdout, NULL, _IONBF, 0)) { perror("Failed to change the buffer of stdout"); return EXIT_FAILURE; } printf(" is"); //" is" is written to stdout //there is no buffering if(setvbuf(stdout, buf, _IOLBF, sizeof buf)) { perror("Failed to change the buffer of stdout"); return EXIT_FAILURE; } printf(" fun."); //The buffer now contains " fun." //but nothing is written to stdout yet putchar('\n'); //stdout is line buffered //everything in the buffer is now written to //stdout along with the newline return EXIT_SUCCESS; }
The output of the above code will be:
Programming is fun.
❮ C++ <cstdio> Library