首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::setbuf

Defined in header <cstdio>

?

?

void setbuf( std::FILE* stream, char* buffer );

?

?

设置用于在C流上执行的I/O操作的内部缓冲区stream...

如果buffer不为空,等效于std::setvbuf(stream, buffer,_IOFBF,BUFSIZ)...

如果buffer为空,等效于std::setvbuf(stream,NULL,_IONBF, 0)关闭缓冲。

参数

stream

-

the file stream to set the buffer to.

buffer

-

pointer to a buffer for the stream to use. If NULL is supplied, the buffering is turned off. If not null, must be able to hold at least BUFSIZ characters

返回值

%280%29

注记

如果BUFSIZ不是适当的缓冲区大小,std::setvbuf可以用来改变它。

std::setvbuf也应该用于检测错误,因为std::setbuf并不表示成功或失败。

此函数只能在stream已与打开的文件相关联,但在任何其他操作%28之前(除失败的调用之外)std::setbuf/std::setvbuf29%。

一个常见错误是将stdin或stdout缓冲区设置为一个数组,该数组的生存期在程序终止之前结束:

二次

代码语言:javascript
复制
int main() {
    char buf[BUFSIZ];
    std::setbuf(stdin, buf);
} // lifetime of buf ends, undefined behavior

二次

setbuf可用于禁用对需要立即输出的流的缓冲。

二次

代码语言:javascript
复制
#include <cstdio>
#include <thread>
#include <chrono>
 
int main()
{
    using namespace std::chrono_literals;
 
    std::setbuf(stdout, NULL); // unbuffered stdout
    std::putchar('a'); // appears immediately on unbuffered stream
    std::this_thread::sleep_for(1s);
    std::putchar('b');
}

二次

产出:

二次

代码语言:javascript
复制
ab

二次

另见

setvbuf

sets the buffer and its size for a file stream (function)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com