std::ios_base::sync_with_stdio

std::ios_base::sync_with_stdio
C++ Input/output library std::ios_base
static bool sync_with_stdio( bool sync = true );
Sets whether the standard C++ streams are synchronized to the standard C streams after each input/output operation.

The standard C++ streams are the following: std::cin, std::cout, std::cerr, std::clog, std::wcin, std::wcout, std::wcerr and std::wclog

The standard C streams are the following: stdin, stdout and stderr

For a standard stream str, synchronized with the C stream f, the following pairs of functions have identical effect:

1) std::fputc(f, c) and str.rdbuf()->sputc(c)
2) std::fgetc(f) and str.rdbuf()->sbumpc()
3) std::ungetc(c, f) and str.rdbuf()->sputbackc(c)
In practice, this means that the synchronized C++ streams are unbuffered, and each I/O operation on a C++ stream is immediately applied to the corresponding C stream’s buffer. This makes it possible to freely mix C++ and C I/O.

In addition, synchronized C++ streams are guaranteed to be thread-safe (individual characters output from multiple threads may interleave, but no data races occur)

If the synchronization is turned off, the C++ standard streams are allowed to buffer their I/O independently, which may be considerably faster in some cases.

By default, all eight standard C++ streams are synchronized with their respective C streams.

If this function is called after I/O has occurred on the standard stream, the behavior is implementation-defined: implementations range from no effect to destroying the read buffer.

Parameters
sync - the new synchronization setting
Return value
synchronization state before the call to the function

Example
Run this code

#include <iostream>
#include <cstdio>

int main()
{
    std::ios::sync_with_stdio(false);
    std::cout << "a\n";
    std::printf("b\n");
    std::cout << "c\n";
}

Possible output:

b
a
c
转:https://en.cppreference.com/w/cpp/io/ios_base/sync_with_stdio

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值