Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/io/c/setvbuf"

From cppreference.com
< cpp‎ | io‎ | c
(Use std namespace for FILE*.)
(+details, +example)
Line 5: Line 5:
 
}}
 
}}
  
Sets the internal buffer of the given file stream {{tt|stream}}.  
+
Changes the the buffering mode of the given file stream {{tt|stream}} as indicated by the argument {{tt|mode}}. In addition,
 +
 
 +
* If if {{tt|buffer}} is a null pointer, resizes of the internal buffer to {{tt|size}}.
 +
* If {{tt|buffer}} is not a null pointer, instructs the stream to use the user-provided buffer of size {{tt|size}} beginning at {{tt|buffer}}. The stream must be closed (with {{lc|fclose}}) before the [[c/language/lifetime|lifetime]] of the array pointed to by {{tt|buffer}} ends. The contents of the array after a successful call to {{tt|setvbuf}} are indeterminate and any attempt to use it is undefined behavior.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | stream | the file stream to set the buffer to}}
+
{{par | stream | the file stream to set the buffer to or null pointer to change size and mode only}}
 
{{par | buffer | pointer to a buffer for the stream to use}}
 
{{par | buffer | pointer to a buffer for the stream to use}}
 
{{par | mode |2= buffering mode to use. It can be one of the following values:
 
{{par | mode |2= buffering mode to use. It can be one of the following values:
Line 22: Line 25:
  
 
===Notes===
 
===Notes===
This function may only be used after {{tt|stream}} has been associated with an open file, but before any other operation.
+
This function may only be used after {{tt|stream}} has been associated with an open file, but before any other operation (other than a failed call to {{lc|std::setbuf}}/{{tt|std::setvbuf}}).
  
{{tt|buffer}} may be null, in which case this call only resizes the internal buffer.
+
Not all {{tt|size}} 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 of stdin or stdout to an array whose lifetime ends before the program terminates:
 +
{{source|1=
 +
int main() {
 +
    char buf[BUFSIZ];
 +
    std::setbuf(stdin, buf);
 +
} // lifetime of buf ends, undefined behavior
 +
}}
 +
 
 +
The default buffer size {{lc|BUFSIZ}} is expected to be the most efficient buffer size for file I/O on the implementation.
 +
 
 +
 
 +
===Example===
 +
{{example|one use case for changing buffer size is when a better size is known
 +
|code=
 +
#include <iostream>
 +
#include <cstdio>
 +
#include <stdlib.h>
 +
#include <sys/stat.h>
 +
 
 +
int main()
 +
{
 +
    std::FILE* fp = std::fopen("test.txt", "r");
 +
    if(!fp) {
 +
      std::perror("fopen"); return 1;
 +
    }
 +
 
 +
    struct stat stats;
 +
    if(fstat(fileno(fp), &stats) == -1) { // POSIX only
 +
        std::perror("fstat"); return 1;
 +
    }
 +
 
 +
    std::cout << "BUFSIZ is " << BUFSIZ << ", but optimal block size is "
 +
              << stats.st_blksize << '\n';
 +
    if(std::setvbuf(fp, NULL, _IOFBF, stats.st_blksize) != 0) {
 +
      perror("setvbuf failed"); // POSIX version sets errno
 +
      return 1;
 +
    }
 +
 
 +
    int ch;
 +
    while((ch=std::fgetc(fp)) != EOF); // read entire file: use truss/strace to
 +
                                      // observe the read(2) syscalls used
 +
}
 +
|p=true
 +
|output=
 +
BUFSIZ is 8192, but optimal block size is 65536
 +
}}
  
 
===See also===
 
===See also===

Revision as of 12:50, 2 February 2015

 
 
 
C-style I/O
Types and objects
Functions
File access
setvbuf

Direct input/output
Unformatted input/output
Formatted input
(C++11)(C++11)(C++11)    
(C++11)(C++11)(C++11)    
 
Defined in header <cstdio>
int setvbuf( std::FILE* stream, char* buffer, int mode, std::size_t size );

Changes the the buffering mode of the given file stream stream as indicated by the argument mode. In addition,

  • If if buffer is a null pointer, resizes of the internal buffer to size.
  • If buffer is not a null pointer, instructs the stream to use the user-provided buffer of size size beginning at buffer. The stream must be closed (with fclose) before the lifetime of the array pointed to by buffer ends. The contents of the array after a successful call to setvbuf are indeterminate and any attempt to use it is undefined behavior.

Contents

Parameters

stream - the file stream to set the buffer to or null pointer to change size and mode only
buffer - pointer to a buffer for the stream to use
mode - buffering mode to use. It can be one of the following values:
_IOFBF full buffering
_IOLBF line buffering
_IONBF no buffering
size - size of the buffer

Return value

0 on success or nonzero on failure.

Notes

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).

Not all size 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 of stdin or stdout 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 size BUFSIZ is expected to be the most efficient buffer size for file I/O on the implementation.


Example

one use case for changing buffer size is when a better size is known

#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <sys/stat.h>
 
int main()
{
    std::FILE* fp = std::fopen("test.txt", "r");
    if(!fp) {
       std::perror("fopen"); return 1;
    }
 
    struct stat stats;
    if(fstat(fileno(fp), &stats) == -1) { // POSIX only
        std::perror("fstat"); return 1;
    }
 
    std::cout << "BUFSIZ is " << BUFSIZ << ", but optimal block size is "
              << stats.st_blksize << '\n';
    if(std::setvbuf(fp, NULL, _IOFBF, stats.st_blksize) != 0) {
       perror("setvbuf failed"); // POSIX version sets errno
       return 1;
    }
 
    int ch;
    while((ch=std::fgetc(fp)) != EOF); // read entire file: use truss/strace to
                                       // observe the read(2) syscalls used
}

Possible output:

BUFSIZ is 8192, but optimal block size is 65536

See also

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 for setvbuf