IO对象的缓冲区的刷新指的是什么?其作用是什么?看了《C++ primer 4th》里的解释如下:
“Each IO object manages a buffer, which is used to hold the data that the program reads and writes. When we write
os << "please enter a value: ";
the literal string is stored in the buffer associated with the stream os. There are several conditions that cause the buffer to be flushed, that is, written to the actual output device or file:……”
难道只是将缓冲区的内容输出出来么?开始我以为是清空缓冲区内的数据,但好像又不是。由于istream和fstream里定义的IO对象都抓取不到其缓冲区的内容。下面我用sstream里定义的IO对象实验了一下:
# include <sstream>
# include <iostream>
using namespace std;
void main()
{
ostringstream o_stream;
for (int ival,i(0); i!=3; ++i) {
o_stream << "Hello " << flush;
}
cout << o_stream.str() << endl;
}
实验的结果看到的是这个:
Hello Hello Hello
也就是说flush操控符刷新了o_stream的缓冲区,但却没清除里面的数据。那刷新缓冲区的作用是什么?可以举出一个缓冲区不刷新而导致结果有误的实例么?
求高手解答,感激不尽……
“Each IO object manages a buffer, which is used to hold the data that the program reads and writes. When we write
os << "please enter a value: ";
the literal string is stored in the buffer associated with the stream os. There are several conditions that cause the buffer to be flushed, that is, written to the actual output device or file:……”
难道只是将缓冲区的内容输出出来么?开始我以为是清空缓冲区内的数据,但好像又不是。由于istream和fstream里定义的IO对象都抓取不到其缓冲区的内容。下面我用sstream里定义的IO对象实验了一下:
# include <sstream>
# include <iostream>
using namespace std;
void main()
{
ostringstream o_stream;
for (int ival,i(0); i!=3; ++i) {
o_stream << "Hello " << flush;
}
cout << o_stream.str() << endl;
}
实验的结果看到的是这个:
Hello Hello Hello
也就是说flush操控符刷新了o_stream的缓冲区,但却没清除里面的数据。那刷新缓冲区的作用是什么?可以举出一个缓冲区不刷新而导致结果有误的实例么?
求高手解答,感激不尽……
解决方案 »
- 关于基于对话框中子窗口传递数据给父窗口问题(新手请教多多帮忙)
- 请教调用可执行文件的函数及其具体应用(昨天问过了但是还是很多不懂)
- 请问写一个MP3播放器需要什么啊??
- vc如何运用Http协议进行XML数据包的传输
- 大难题?如何创建象VC Assistant那样的插件Addin,加到VC中能响应鼠标消息和右键菜单消息???
- C++2012MFC和sql server2012链接问题,大神看我哪里有错啊,弄了4天了还没连上
- 这个问题如何解决呀?“error MIDL2041 : [out] only parameter cannot be an unsized string : [ Type 'LPTSTR' ( Parameter 'a_Text' )
- tc与vc在结构定义上的区别
- hook的经典问题 急在线等
- vc win32 application对话框中edit control 把password属性设为true后,抛异常
- 贪婪算法
- VS 2008 DirectShow(Directx 9.0b) 配置 的问题
下列情形会激发缓冲区的刷新:
1、缓冲区满时;
2、执行flush语句;
3、执行endl语句;
4、封锁文件。
可见,缓冲区满或封锁文件时城市刷新缓冲区,进行真正的I/O操作。此外,在C++中,我们可以使用flush函数来刷新缓冲区(执行I/O操作并清空缓冲区),如:
cout<<flush; //将显存的内容当即输出到显示器长进行显示
endl节制符的浸染是将光标移动到输出设备中下一行开首处,而且清空缓冲区。
cout<<endl;
相当于
cout<<”\n” <<flush