参考链接

参考链接2

Buffers are normally maintained by the operating system, which determines the optimal time to write the data automatically to disk: when a buffer is full, when a stream is closed, or when a program terminates normally without closing the stream.

当我们对文件流进行操作的时候,它们与一个streambuf 类型的缓存(buffer)联系在一起。这个缓存(buffer)实际是一块内存空间,作为流(stream)和物理文件的媒介。例如,对于一个输出流, 每次成员函数put  (写一个单个字符)被调用,这个字符不是直接被写入该输出流所对应的物理文件中的,而是首先被插入到该流的缓存(buffer)中。

当缓存被排放出来(flush)时,它里面的所有数据或者被写入物理媒质中(如果是一个输出流的话),或者简单的被抹掉(如果是一个输入流的话)。这个过程称为同步(synchronization),它会在以下任一情况下发生:

1.进程/线程/程序正常结束时,将刷新所有的输出缓冲区。

2.缓冲区满了,在这种情况下,缓冲区将会在写下一个值之前刷新。

3.用操纵符显示地刷新缓冲区,如用endl。

4.在每次输出操作执行完毕后,用unitbuf操纵符设置流的内部状态,从而清空缓冲区。

5.默认情况下cin与cout是关联的,在cin时将刷新输出缓冲区。

====================================

fflush Defined in header <stdio.h>
int fflush( FILE *stream );
For output streams (and for update streams on which the last operation was output), writes any unwritten data from the stream's buffer to the associated output device.
If stream is a null pointer, all open output streams are flushed, including the ones manipulated within library packages or otherwise not directly accessible to the program.

eg.
fflush(stdout);

====================================

std::flush Defined in header <ostream>
template< class CharT, class Traits >
std::basic_ostream<CharT, Traits>& flush( std::basic_ostream<CharT, Traits>& os );
An explicit flush of std::cout is also necessary before a call to std::system, if the spawned process performs any screen I/O (a common example is std::system("pause") on Windows). In most other usual interactive I/O scenarios, std::endl is redundant when used with std::cout because any input from std::cin, output to std::cerr, or program termination forces a call to std::cout.flush().
When a complete line of output needs to be flushed, the std::endl manipulator may be used.
When every output operation needs to be flushed, the std::unitbuf manipulator may be used.

eg.
std::cout.flush();

====================================

std::ios_base::sync_with_stdio
std::ios_base::sync_with_stdio(true); //默认情况同步,cout与stdout共享同一缓冲区。
std::ios_base::sync_with_stdio(false); //取消同步,cout与printf不再共享同一缓冲区,混用cout与printf会乱序。

正是这种同步,导致cin/cout比scanf/printf速度慢。
https://www.byvoid.com/blog/fast-readfile

====================================

std::basic_ios::tie
A tied stream is an output stream which is synchronized with the sequence controlled by the stream buffer (rdbuf()), that is, flush() is called on the tied stream before any input/output operation on *this.
默认情况下,cin与cout是绑定的,cin会刷新cout的缓冲区。理论上该情况下,cin与cout可共享一个缓冲区,但似乎没有这么实现的。
std::cin.tie(0); //tie在绑定了cout后,会保证在cin执行前刷新cout的缓冲区。cin.tie(0)解除了绑定,则并不保证cout在cin之前刷新,但也不保证不会刷新。

https://stackoverflow.com/questions/14052627/why-do-we-need-to-tie-stdcin-and-stdcout

C/C++缓冲区刷新问题的更多相关文章

  1. 【VS开发】【C/C++开发】printf缓冲区刷新

    printf之缓冲区小结: 今天调试程序,发现了一个有趣的现象,printf函数没有按照预期的结果输出重复的字符串,单步调试显示代码的确走到了打印屏幕的分支,没有显示不由得想到了是不是缓冲区去刷新的问 ...

  2. C++输入输出缓冲区的刷新问题

    当我们对文件流进行操作的时候,它们与一个streambuf 类型的缓存(buffer)联系在一起.这个缓存(buffer)实际是一块内存空间,作为流(stream)和物理文件的媒介.例如,对于一个输出 ...

  3. php缓冲区详解

    什么是缓冲区(buffer)? 简单而言,缓冲区的作用就是,把输入或者输出的内容先放进内存,而不显示或者读取.至于为什么要有缓冲区,这是一个很广泛的问题,如果有兴趣,可以在网山找下资料. 其实缓冲区最 ...

  4. PHP的输出缓冲区(转)

    什么是缓冲区?简单而言,缓冲区的作用就是,把输入或者输出的内容先放进内存,而不显示或者读取.至于为什么要有缓冲区,这是一个很广泛的问题,如果有兴趣,可以在网山找下资料.其实缓冲区最本质的作用就是,协调 ...

  5. C输入输出函数与缓冲区

    #转 对C语言输入输出流和缓冲区的深入理解C语言缓冲区(缓存)详解缓冲区又称为缓存,它是内存空间的一部分.也就是说,在内存空间中预留了一定的存储空间,这些存储空间用来缓冲输入或输出的数据,这部分预留的 ...

  6. c/c++ 输入输出缓冲区

    关于缓冲区的详细介绍,请参考 C++编程对缓冲区的理解 CPP的输入输出流和缓冲区 c++输出缓冲区刷新   (1)c++中cin.cout,cerr和c的stdin.stdout.stderr都是同 ...

  7. 关于header('location:url')的一些说明,php缓冲区

    网上搜索header('location:url')的用法,得到如下三个结论: 1. location和“:”号间不能有空格,否则会出错. 2. 在用header前不能有任何的输出. 3. heade ...

  8. 同步内核缓冲区sync、fsync和fdatasync函数

    转自http://www.2cto.com/os/201409/339460.html 同步内核缓冲区 1.缓冲区简介 人生三大错觉之一:在调用函数write()时,我们认为该函数一旦返回,数据便已经 ...

  9. PHP的输出缓冲区

    什么是缓冲区?简单而言,缓冲区的作用就是,把输入或者输出的内容先放进内存,而不显示或者读取.至于为什么要有缓冲区,这是一个很广泛的问题,如果有兴趣,可以在网山找下资料.其实缓冲区最本质的作用就是,协调 ...

随机推荐

  1. Youtube高清视频下载的3种方法

    经常看视频的朋友都听说或使用过youtube,  它是一个综合性的视频网站,包含的内容多种多样,能满足不同的人的需求,最要的是广告少,资源良心,不像有些网站,动不动就是1分种以上的长广告.有些因为工作 ...

  2. 第1章 Linux命令行简介

    1.1 Linux命令行概述 1.2 在Linux命令行下查看命令帮助 1.3 Linux关机.重启.注销命令 1.4 老男孩的运维思想 1.1 Linux命令行概述 1.1.1 Linux命令行的作 ...

  3. spring boot 使用及最佳实践

    第一部分,spring boot 文档 Spring boot的使用 使用maven进行构建 用户可以通过继承spring-boot-starter-parent来获取默认的依赖. l  默认java ...

  4. Docker--Dockerfile引用及指令集的功能用法

    Dockerfile引用的官网文档:https://docs.docker.com/engine/reference/builder/ 编写Dockerfiles的最佳实践的官网文档:https:// ...

  5. JetBrains激活 PyCharm | IntelliJ IDEA | CLion | WebStorm...

    最近,JetBrains的IDE火了起来,身为学Java的人,放弃了Eclipse,选择了Idea,还真有点不舍得呢... 虽然Idea不错(在我看来,比Eclipse好用),但是,人家是收费的呀.. ...

  6. 基于KVM的H3C云计算平台CAS运维经验

  7. Vue02

    3.Vue对象提供的属性功能 过滤器,就是vue允许开发者自定义的文本格式化函数,可以使用在两个地方:输出内容和操作数据中. 定义过滤器的方式有两种. 1 使用Vue.filter()进行全局定义 V ...

  8. python实现简单线性回归

    之前推导了一元线性回归和多元线性回归,今天就用python来实现一下一元线性回归 先看下之前推导的结果   ,  第一种是用循环迭代的计算方法.这里的x,y是numpy中的array类型 def su ...

  9. Metasploit拿Shell

    进入metasploit系统 msfconsole Nmap端口扫描 nmap –sV IP(或者域名),如果机器设置有防火墙禁ping,可以使用nmap -P0(或者-Pn) –sV IP(或者域名 ...

  10. telnet命令详解

    基础命令学习目录 原文链接:https://www.cnblogs.com/PatrickLiu/p/8556762.html telnet命令用于登录远程主机,对远程主机进行管理.telnet因为采 ...