std:: setbuf
| 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 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<cstdio>
|
||
|
void
setbuf
(
std::
FILE
*
stream,
char
*
buffer
)
;
|
||
设置用于在 C 流 stream 上执行 I/O 操作的内部缓冲区。
如果 buffer 不为空,则等价于 std:: setvbuf ( stream, buffer, _IOFBF , BUFSIZ ) 。
如果 buffer 为空,相当于 std:: setvbuf ( stream, nullptr, _IONBF , 0 ) ,这将关闭缓冲功能。
目录 |
参数
| stream | - | 要设置缓冲区的文件流 |
| buffer | - |
指向供流使用的缓冲区指针。如果提供空指针,则关闭缓冲。若非空,必须能够容纳至少
BUFSIZ
个字符
|
返回值
(无)
注释
如果 BUFSIZ 不是合适的缓冲区大小,可以使用 std::setvbuf 来修改它。
std::setvbuf
也应当用于检测错误,因为
std::setbuf
不会指示成功或失败。
此函数仅可在
stream
已关联打开文件后使用,但必须在任何其他操作之前(除失败的
std::setbuf
/
std::setvbuf
调用外)。
一个常见错误是将 stdin 或 stdout 的缓冲区设置为在程序终止前生命周期就已结束的数组:
int main() { char buf[BUFSIZ]; std::setbuf(stdin, buf); } // buf的生命周期结束,未定义行为
示例
std::setbuf
可用于在需要即时输出的流上禁用缓冲。
#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'); }
输出:
ab
参见
|
设置文件流的缓冲区及其大小
(函数) |
|
|
C 文档
关于
setbuf
|
|