写入文件的中间(不覆盖数据) [英] Writing to the middle of the file (without overwriting data)

查看:134
本文介绍了写入文件的中间(不覆盖数据)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows中,是否可以通过API写入文件的中间,而不覆盖任何数据,而无需在此之后重写所有内容?

In windows is it possible through an API to write to the middle of a file without overwriting any data and without having to rewrite everything after that?

如果可能然后我相信它会明显碎片的文件;我可以在它成为一个严重的问题之前做多少次?

If it's possible then I believe it will obviously fragment the file; how many times can I do it before it becomes a serious problem?

如果不可能,通常采取什么方法/解决方法?

If it's not possible what approach/workaround is usually taken? Re-writing everything after the insertion point becomes prohibitive really quickly with big (ie, gigabytes) files.

推荐答案

我不知道有什么办法做这个>如果您需要的中间结果是可由除编辑者之外的其他应用程序使用的平面文件。如果你想要生成一个平面文件,你必须从更改点更新到文件结束,因为它只是一个顺序文件。

I'm unaware of any way to do this if the interim result you need is a flat file that can be used by other applications other than the editor. If you want a flat file to be produced, you will have to update it from the change point to the end of file, since it's really just a sequential file.

但是斜体是有很好的理由。如果你可以控制文件格式,你有一些选项。某些版本的MS Word具有快速保存功能,它们不会重写整个文档,而是将增量记录附加到文件末尾。然后,当重新读取文件,它应用所有的deltas顺序,所以你最后得到的是正确的文件。如果保存的文件必须立即用于不了解文件格式的其他应用程序,这显然不会工作。

But the italics are there for good reason. If you can control the file format, you have some options. Some versions of MS Word had a quick-save feature where they didn't rewrite the entire document, rather they appended a delta record to the end of the file. Then, when re-reading the file, it applied all the deltas in order so that what you ended up with was the right file. This obviously won't work if the saved file has to be usable immediately to another application that doesn't understand the file format.

我建议的是将文件存储为文本。使用中间表单,您可以有效地编辑和保存,然后有一个步骤,将其转换为可用的文本文件不经常(例如,在编辑器退出)。这样,用户可以节省尽可能多的,因为时间昂贵的操作不会有太多的影响。

What I'm proposing there is to not store the file as text. Use an intermediate form that you can efficiently edit and save, then have a step which converts that to a usable text file infrequently (e.g., on editor exit). That way, the user can save as much as they want but the time-expensive operation won't have as much of an impact.

除此之外,还有一些其他

Beyond that, there are some other possibilities.

内存映射(而不是加载)文件可以提供效率,这将加快速度。你可能仍然需要重写到文件的末尾,但它会发生在操作系统的较低级别。

Memory-mapping (rather than loading) the file may provide efficiences which would speed things up. You'd probably still have to rewrite to the end of the file but it would be happening at a lower level in the OS.

如果主要原因你想要快速保存是让用户继续工作(而不是让文件可用于另一个应用程序),您可以将保存操作放到单独的线程中,并立即将控制权返回给用户。然后,您需要在两个线程之间同步,以防止用户修改数据仍保存到磁盘。

If the primary reason you want fast save is to start letting the user keep working (rather than having the file available to another application), you could farm the save operation out to a separate thread and return control to the user immediately. Then you would need synchronisation between the two threads to prevent the user modifying data yet to be saved to disk.

这篇关于写入文件的中间(不覆盖数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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