C/C++缓冲区刷新问题
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++缓冲区刷新问题的更多相关文章
- 【VS开发】【C/C++开发】printf缓冲区刷新
printf之缓冲区小结: 今天调试程序,发现了一个有趣的现象,printf函数没有按照预期的结果输出重复的字符串,单步调试显示代码的确走到了打印屏幕的分支,没有显示不由得想到了是不是缓冲区去刷新的问 ...
- C++输入输出缓冲区的刷新问题
当我们对文件流进行操作的时候,它们与一个streambuf 类型的缓存(buffer)联系在一起.这个缓存(buffer)实际是一块内存空间,作为流(stream)和物理文件的媒介.例如,对于一个输出 ...
- php缓冲区详解
什么是缓冲区(buffer)? 简单而言,缓冲区的作用就是,把输入或者输出的内容先放进内存,而不显示或者读取.至于为什么要有缓冲区,这是一个很广泛的问题,如果有兴趣,可以在网山找下资料. 其实缓冲区最 ...
- PHP的输出缓冲区(转)
什么是缓冲区?简单而言,缓冲区的作用就是,把输入或者输出的内容先放进内存,而不显示或者读取.至于为什么要有缓冲区,这是一个很广泛的问题,如果有兴趣,可以在网山找下资料.其实缓冲区最本质的作用就是,协调 ...
- C输入输出函数与缓冲区
#转 对C语言输入输出流和缓冲区的深入理解C语言缓冲区(缓存)详解缓冲区又称为缓存,它是内存空间的一部分.也就是说,在内存空间中预留了一定的存储空间,这些存储空间用来缓冲输入或输出的数据,这部分预留的 ...
- c/c++ 输入输出缓冲区
关于缓冲区的详细介绍,请参考 C++编程对缓冲区的理解 CPP的输入输出流和缓冲区 c++输出缓冲区刷新 (1)c++中cin.cout,cerr和c的stdin.stdout.stderr都是同 ...
- 关于header('location:url')的一些说明,php缓冲区
网上搜索header('location:url')的用法,得到如下三个结论: 1. location和“:”号间不能有空格,否则会出错. 2. 在用header前不能有任何的输出. 3. heade ...
- 同步内核缓冲区sync、fsync和fdatasync函数
转自http://www.2cto.com/os/201409/339460.html 同步内核缓冲区 1.缓冲区简介 人生三大错觉之一:在调用函数write()时,我们认为该函数一旦返回,数据便已经 ...
- PHP的输出缓冲区
什么是缓冲区?简单而言,缓冲区的作用就是,把输入或者输出的内容先放进内存,而不显示或者读取.至于为什么要有缓冲区,这是一个很广泛的问题,如果有兴趣,可以在网山找下资料.其实缓冲区最本质的作用就是,协调 ...
随机推荐
- Python学习之路:一天搞定基础部分
~代表省略的内容,如变量名.字符串等等 1.Pyhton中比较特别的运算: **:代表指数运算,例如2**3 = 8 //:代表整除运算,这一点和Java不同 2.Python的注释: #:单行注释 ...
- linux shell 完成批量压缩文件
首先得到文件列表 使用 list -1 注意是1 不是l 然后是用一个循环内包装zip代码 #!/bin/bash list=`` for var in $list do echo $var zip ...
- iFIERO - (一) 宇宙大战 SPACE BATTLE — 场景SCENE、SpriteKit精灵、PARTICLE粒子及背景音乐
开始游戏教程前,首先介绍一下SpriteKit是什么?SpriteKit提供了一个图形渲染和动画的基础结构,你可以使用它让任意类型的纹理图片或者精灵动起来.SpriteKit使用渲染循环,利用图形硬件 ...
- 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number
[Q7] 把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...
- SQL Server上DBLINK的创建,其实很简单!(上)
今天给大家来分享一下跨服务器操作数据库,还是以SQL Server的管理工具(SSMS)为平台进行操作. 什么是跨服务器操作? 跨服务器操作就是可以在本地连接到远程服务器上的数据库,可以在对方的数据库 ...
- Scikit-learn数据变换
转载自:https://blog.csdn.net/Dream_angel_Z/article/details/49406573 本文主要是对照scikit-learn的preprocessing章节 ...
- Java non-javadoc
Java注释 non-javadoc 表示该处没有自己的注释, @see javax.servlet.Servlet#init() 参考see后面的链接 /* * (non-javadoc) * @s ...
- CS224n学习笔记1——深度自然语言处理
一.什么是自然语言处理呢? 自然语言处理是计算机科学家提出的名字,本质上与计算机语言学是同义的,它跨越了计算机学.语言学以及人工智能学科. 自然语言处理是人工智能的一个分支,在计算机研究领域中,也有其 ...
- python的多路复用实现聊天群
在我的<python高级编程和异步io编程>中我讲解了socket编程,这里贴一段用socket实现聊天室的功能的源码,因为最近工作比较忙,后期我会将这里的代码细节分析出来,目前先把代码贴 ...
- python判断文件和文件夹是否存在、没有则创建文件夹
原文出处:https://www.cnblogs.com/hushaojun/p/4533241.html >>> import os >>> os.path.ex ...