关于fstream缓冲区,flush()和sync()之间有什么区别? [英] What is the difference between flush() and sync() in regard to fstream buffers?

查看:1044
本文介绍了关于fstream缓冲区,flush()和sync()之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关I / O的 cplusplus.com教程。最后,它说fstream缓冲区与磁盘上的文件同步

I was reading the cplusplus.com tutorial on I/O. At the end, it says fstream buffers are synchronized with the file on disc


明确地带有操纵器:当使用某些操纵器时在
流上,发生显式同步。这些操纵器
是:flush和endl。

Explicitly, with manipulators: When certain manipulators are used on streams, an explicit synchronization takes place. These manipulators are: flush and endl.


明确地,使用成员函数sync()::调用
流的成员函数sync()(不带参数)会导致
立即同步。如果流没有关联的缓冲区或发生故障,则此函数返回等于
-1的int值。否则(如果流缓冲区已成功同步),它
返回0。

Explicitly, with member function sync(): Calling stream's member function sync(), which takes no parameters, causes an immediate synchronization. This function returns an int value equal to -1 if the stream has no associated buffer or in case of failure. Otherwise (if the stream buffer was successfully synchronized) it returns 0.

除了一些其他隐式情况(如破坏和stream.close())

in addition to a few other implicit cases ( such as destruction and stream.close() )

在我的代码中,我一直使用flush()。

In my code, I've always used flush().

关于std :: flush()的文档:

Documentation on std::flush():


刷新流缓冲区

将与流
关联的缓冲区同步到其受控输出序列。这实际上意味着缓冲区中的所有
未写字符都应尽快写入其受控的
输出序列中(刷新)。

Synchronizes the buffer associated with the stream to its controlled output sequence. This effectively means that all unwritten characters in the buffer are written to its controlled output sequence as soon as possible ("flushed").

std :: streambuf :: sync():

Documentation on std::streambuf::sync():


将输入缓冲区与字符源同步

调用该命令以使流缓冲区与受控序列(例如文件流中的文件)同步。公共成员函数pubsync调用此受保护的成员函数来执行此操作。

It is called to synchronize the stream buffer with the controlled sequence (like the file in the case of file streams). The public member function pubsync calls this protected member function to perform this action.

如果这是一个新手问题,请见谅;我是菜鸟。

Forgive me if this is a newbie question; I am a noob.

推荐答案

basic_ostream :: flush
这是一个 non-virtual 函数,将未提交的更改写入基础缓冲区。如果发生错误,它将在使用的流对象中设置一个错误标志。这是因为返回值是对流本身的引用,以允许链接。

basic_ostream::flush This is a non-virtual function which writes uncommited changes to the underlying buffer. In case of error, it sets an error flag in the used stream object. This is because the return value is a reference to the stream itself, to allow chaining.

basic_filebuf :: sync
这是 virtual 函数,该函数将所有未完成的更改写入基础文件,并返回错误代码以表示成功或失败。

basic_filebuf::sync This is a virtual function which writes all pending changes to the underlying file and returns an error code to signal success or failure.

endl
当将其应用于 ostream 时,将写入'\n'到流中,然后在该流上调用 flush

endl This, when applied to an ostream, writes an '\n' to the stream and then calls flush on that stream.

因此,本质上是: flush 是任何流的更通用的功能,而 sync 明确绑定到文件。 flush 是非虚拟的,而 sync 是虚拟的。在继承的情况下,这改变了如何通过指针(指向基类)使用它们。此外,它们在报告错误的方式上也有所不同。

So, essentially: flush is a more general function for any stream, whereas sync is explicitly bound to a file. flush is non-virtual, whereas sync is virtual. This changes how they can be used via pointers (to base class) in the case of inheritance. Furthermore, they differ in how they report errors.

这篇关于关于fstream缓冲区,flush()和sync()之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆