强制在Windows上的流文件刷新 [英] Force ofstream file flush on Windows

查看:499
本文介绍了强制在Windows上的流文件刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ofstream 将数据写入文件。我经常在文件上调用 flush ,但是备份文件在那时并不总是得到更新。我假设这是一个操作系统级的缓存或MSVC库内的东西。

I'm using an ofstream to write data to a file. I regularly call flush on the file but the backing file doesn't always get updated at that time. I assume this is related to an OS-level cache, or something inside the MSVC libraries.

我需要一种方法来让数据正确刷新。最好写入光盘,但至少足够使得从另一个程序的复制操作将看到所有数据直到刷新点。

I need a way to have the data properly flush at that point. Preferably written to disc, but at least enough such that a copy operation from another program would see all data up to the flush point.

我可以使用什么API来执行此操作?

What API can I use to do this?

推荐答案

FlushFileBuffers 将刷新Windows写入文件缓存并将其写入文件。请注意,如果重复调用,可能会很慢。

FlushFileBuffers will flush the Windows write file cache and write it to a file. Be aware it can be very slow if called repeatedly.

我也发现这 KB 文章中介绍了 _ commit ()。这可能对你更有用,因为你正在使用ofstream。

I also found this KB article which describes the use of _commit(). This might be more useful to you since you are using ofstream.

CXXFileBuf.flush();
_commit(CXXFileBuf.rdbuf()->fd());

这篇关于强制在Windows上的流文件刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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