参考链接

参考链接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. TensorFlow Python2.7环境下的源码编译(三)编译

    一.源代码编译 这里要为仅支持 CPU 的 TensorFlow 构建一个 pip 软件包,需要调用以下命令: $ bazel build --cxxopt="-D_GLIBCXX_USE_ ...

  2. Proe/Creo 零件库mnu文件制作批处理

    proe零件库自定义时需要菜单文件mnu,百度了下网上还没有人制作,偶然间Google时在PTC论坛上看到一德国人分享了自己制作的bat文件用于对文件夹(及子文件夹)产生mnu文件,我在将他的文件翻译 ...

  3. c语言数字图像处理(七):频率域滤波

    代码运行了两个小时才出的结果,懒得测试了,这一部分先鸽了,等对DFT算法进行优化后再更

  4. js数组知识点总结及经典笔试题

    1.判断数组 这是笔试里经常会出现的知识考察点,总结一下 (1)Array.isArray()方法判断 var a=[]; Array.isArray(a) //返回true var b='hello ...

  5. 理解粒子滤波(particle filter)

    1)初始化阶段-提取跟踪目标特征 该阶段要人工指定跟踪目标,程序计算跟踪目标的特征,比如可以采用目标的颜色特征.具体到Rob Hess的代码,开始时需要人工用鼠标拖动出一个跟踪区域,然后程序自动计算该 ...

  6. shutil模块详解

    python常用模块目录 注意:shutil经常遇到路径需要转义一下才能执行,在字符串前面加 r转义  r" " 1.shutil常用方法 import shutil# 删除目录 ...

  7. basename命令详解

    基础命令学习目录首页 摘要:前言bashname命令用于获取路径中的文件名或路径名(获取的时候叶子节点的元素内容)常见用法举例basenamepath获取末尾的文件名或路径名1:[aliyunzixu ...

  8. nginx中location详解

    Location block 的基本语法形式是: location [=|~|~*|^~|@] pattern { ... } [=|~|~*|^~|@] 被称作 location modifier ...

  9. 利用Cocoapods创建基于SVN的私有库podspec

    由于项目年后要进行组件化,考虑到公司内部实现的一些私有组件,不对外公开,而又想在不同项目中使用,该怎么办呢?由于cocoapods有了强大的功能,可以自己创建podspec,更可以设置私有的库.那么利 ...

  10. CS小分队第二阶段冲刺站立会议(6月3日)

    昨日成果:完成了主界面按钮移动交换位置 遇到问题:最后的时候发现仅交换了按钮在数据库中的信息,对于按钮的链接忘记交换了 今日计划:解决这个问题,对这个冲刺阶段的成果进行整理