如何在C ++中减小fstream文件的大小 [英] How to reduce the size of a fstream file in C++

查看:109
本文介绍了如何在C ++中减小fstream文件的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 11中切断fstream文件结尾的最佳方法是什么

What is the best way to cut the end off of a fstream file in C++ 11

我正在编写一个数据持久性类来为我的音频编辑器存储音频.我选择使用fstream(可能是个坏主意)来创建随机访问二进制读写文件.

I am writing a data persistence class to store audio for my audio editor. I have chosen to use fstream (possibly a bad idea) to create a random access binary read write file.

每次我在文件中录制一些声音时,我都会将其粘贴到该文件的末尾.另一个内部数据结构/文件,包含指向音频文件的指针,并跟踪编辑.
当我撤消录制动作然后执行其他操作时,音频文件的最后一位变得无关紧要.在文档的当前状态中未引用它,并且您无法将自己重做回到可以再次看到它的状态.因此,我想将文件的这一部分切掉,并从新的一端开始记录.我不需要在中间切掉咬痕,只需在末端切掉.

Each time I record a little sound into my file I simply tack it onto the end of this file. Another internal data structure / file, contains pointers into the audio file and keeps track of edits.
When I undo a recording action and then do something else the last bit of the audio file becomes irrelevant. It is not referenced in the current state of the document and you cannot redo yourself back to a state where you can ever see it again. So I want to chop this part of the file off and start recording at the new end. I don’t need to cut out bitts in the middle, just off the end.

当用户退出时,此文件将保留并在他们再次打开项目时重新加载.

When the user quits this file will remain and be reloaded when they open the project up again.

在我的应用程序中,我希望用户可以一直这样做,并且能够执行此操作可以为我节省多达30%的文件大小.该文件将很长,可能非常长,因此,每次发生这种情况时,将其重写到另一个文件中都是不可行的选择.

In my application I expect the user to do this all the time and being able to do this might save me as much as 30% of the file size. This file will be long, potentially very, very long, so rewriting it to another file every time this happens is not a viable option.

在用户保存时重写它可能是一种选择,但仍然没有那么吸引人.

Rewriting it when the user saves could be an option but it is still not that attractive.

我可以在开头添加一个值,说该文件应该保留多长时间,然后覆盖结尾以回收空间,但同时.如果我想在发生崩溃的情况下不断更新数据存储文件,这意味着我将一遍又一遍地重写启动文件.我担心这可能对闪存驱动器不利.通过分析指针文件,我还可以重新计算负载文件的有用部分的结尾,但与此同时,我可能会浪费所有空间,这很复杂.

I could stick a value at the start that says how long the file is supposed to be and then overwrite the end to recycle the space but in the mean time. If I wanted to continually update the data store file in case of crash this would mean I would be rewriting the start over and over again. I worry that this might be bad for flash drives. I could also recomputed the end of the useful part of the file on load, by analyzing the pointer file but in the mean time I would be wasting all that space potentially, and that is complicated.

在fstream API中是否对此有一个简单的调用?

Is there a simple call for this in the fstream API?

我使用了错误的库吗?请注意,我想坚持使用我喜欢的通用STL,这样我就可以使代码尽可能地跨平台.

Am I using the wrong library? Note I want to stick to something generic STL I preferred, so I can keep the code as cross platform as possible.

我似乎无法在文档中找到它,并且已经浏览了许多小时.它不是天涯海角,但会使它变得更简单,更有效.也许我只是以某种方式想念它.

I can’t seem to find it in the documentation and have looked for many hours. It is not the end of the earth but would make this a little simpler and potentially more efficient. Maybe I am just missing it somehow.

感谢您的帮助安德烈(Andre)

Thanks for your help Andre’

推荐答案

fstream API中是否对此有一个简单的调用?

Is there a simple call for this in the fstream API?

如果您具有C ++ 17编译器,请使用 std :: filesystem:: resize_file .在以前的标准中,标准库中没有这样的东西.

If you have C++17 compiler then use std::filesystem::resize_file. In previous standards there was no such thing in standard library.

使用较旧的编译器...在Windows上,您可以使用 SetFilePoinerEx 将当前位置设置为所需的大小,然后调用 truncate ftruncate .如果您想要可移植的代码,则可以使用 Boost.Filesystem .将来,从它最简单地迁移到 std :: filesystem ,因为 std :: filesystem 主要是基于它指定的.

With older compilers ... on Windows you can use SetFilePoiner or SetFilePoinerEx to set the current position to the size you want, then call SetEndOfFile. On Unixes you can use truncate or ftruncate. If you want portable code then you can use Boost.Filesystem. From it is simplest to migrate to std::filesystem in the future because the std::filesystem was mostly specified based on it.

这篇关于如何在C ++中减小fstream文件的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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