| 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> | ||
int setvbuf(std::FILE* stream,char* buffer,int mode,std::size_t size); | ||
Changes the buffering mode of the given file streamstream as indicated by the argumentmode. In addition,
Contents |
| stream | - | the file stream to set the buffer to | ||||||
| buffer | - | pointer to a buffer for the stream to use or null pointer to change size and mode only | ||||||
| mode | - | buffering mode to use. It can be one of the following values:
| ||||||
| size | - | size of the buffer |
0 on success or nonzero on 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).
Not allsize bytes will necessarily be used for buffering: the actual buffer size is usually rounded down to a multiple of 2, 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 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
The default buffer sizeBUFSIZ is expected to be the most efficient buffer size for file I/O on the implementation, but POSIXfstat often provides a better estimate.
One use case for changing buffer size is when a better size is known.
#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");returnEXIT_FAILURE;} struct stat stats;if(fstat(fileno(fp),&stats)==-1)// POSIX only{std::perror("fstat");returnEXIT_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 errnoreturnEXIT_FAILURE;} // Read entire file: use truss/strace to observe the read(2) syscalls usedfor(int ch;(ch=std::fgetc(fp))!=EOF;){} std::fclose(fp);returnEXIT_SUCCESS;}
Possible output:
BUFSIZ is 8192, but optimal block size is 65536
| sets the buffer for a file stream (function)[edit] | |
[virtual] | provides user-supplied buffer or turns this filebuf unbuffered (virtual protected member function of std::basic_filebuf<CharT,Traits>)[edit] |
C documentation forsetvbuf | |