Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | io‎ | c
m (fmt, {{c}}, headers sorted, langlinks)
m (fmt)
 
Line 23: Line 23:
 
If {{lc|BUFSIZ}} is not the appropriate buffer size, {{lc|std::setvbuf}} can be used to change it.
 
If {{lc|BUFSIZ}} is not the appropriate buffer size, {{lc|std::setvbuf}} can be used to change it.
  
{{lc|std::setvbuf}} should also be used to detect errors, since {{lc|std::setbuf}} does not indicate success or failure.
+
{{lc|std::setvbuf}} should also be used to detect errors, since {{tt|std::setbuf}} does not indicate success or failure.
  
This function may only be used after {{c|stream}} has been associated with an open file, but before any other operation (other than a failed call to {{lc|std::setbuf}}/{{lc|std::setvbuf}}).
+
This function may only be used after {{c|stream}} has been associated with an open file, but before any other operation (other than a failed call to {{tt|std::setbuf}}/{{lc|std::setvbuf}}).
  
A common error is setting the buffer of {{tt|stdin}} or {{tt|stdout}} to an array whose lifetime ends before the program terminates:
+
A common error is setting the buffer of {{lc|stdin}} or {{lc|stdout}} to an array whose lifetime ends before the program terminates:
 
{{source|1=
 
{{source|1=
 
int main()
 
int main()
Line 38: Line 38:
 
===Example===
 
===Example===
 
{{example
 
{{example
|{{lc|std::setbuf}} may be used to disable buffering on streams that require immediate output.
+
|{{tt|std::setbuf}} may be used to disable buffering on streams that require immediate output.
 
|code=
 
|code=
 
#include <chrono>
 
#include <chrono>

Latest revision as of 11:35, 13 September 2023

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

Direct input/output
Unformatted input/output
Formatted input
(C++11)(C++11)(C++11)    
(C++11)(C++11)(C++11)    
 
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 stream stream.

If buffer is not null, equivalent to std::setvbuf(stream, buffer, _IOFBF, BUFSIZ).

If buffer is null, equivalent to std::setvbuf(stream, nullptr, _IONBF, 0), which turns off buffering.

Contents

[edit] Parameters

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 least BUFSIZ characters

[edit] Return value

(none)

[edit] 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:

int main()
{
    char buf[BUFSIZ];
    std::setbuf(stdin, buf);
} // lifetime of buf ends, undefined behavior

[edit] Example

std::setbuf may be used to disable buffering on streams that require immediate output.

#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');
}

Output:

ab

[edit] See also

sets the buffer and its size for a file stream
(function) [edit]
C documentation for setbuf