C ++ fstream从选定的点擦除文件内容 [英] C++ fstream Erase the file contents from a selected Point

查看:164
本文介绍了C ++ fstream从选定的点擦除文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从选定的点(C ++ fstream)擦除文件内容,我应该使用哪个函数?

code> objects ,我需要在文件中间删除这些 objects

解决方案

C ++没有标准的机制来截断给定点上的文件。你要么重新创建文件(用 ios :: trunc 打开并写入你想保留的内容)或者使用特定于OS的API调用( Unix上的SetEndOfFile ,Unix上的 truncate ftruncate )。
$ b

编辑:删除文件中间的东西是一个非常危险的业务。在考虑其他选择之前,我会尝试使用像SQLite这样的无服务器数据库引擎来存储序列化的对象。更好的是,我将按照预期使用SQLite,将这些对象所需的数据存储在适当的模式中。



编辑2:声明需要原始文件访问...



一般来说,不要从文件中间删除数据。如果可以将对象序列化为磁盘上的固定大小,则可以将它们作为记录进行处理,而不是试图删除数据,而是使用一个将文件中的记录编入索引的表。例如,如果你按顺序写四条记录,表格将保存 [0,1,2,3] 。为了删除第二条记录,只需从表中删除条目: [0,2,3] 。至少有两种方式可以重复使用表中留下的空位:


  1. 在每次插入时,扫描第一个未使用的索引并写入对象在相应的记录位置。然而,随着文件的增长,这将变得更加昂贵。

  2. 保留一个空闲列表。作为一个单独的变量存储最近释放的记录的索引。在该记录所占用的空间中编码之前释放的记录的索引,等等。这维护了一个方便的空闲记录链接列表,而只需要一个附加数字的空间。然而,使用它更复杂,并且在删除和插入时需要额外的磁盘I / O。

如果对象可以不会被序列化为一个固定的长度,那么这种方法变得非常困难得多。变长记录管理代码是非常复杂的。

最后,如果问题陈述需要在磁盘上保持记录顺序,那么这是一个愚蠢的问题陈述,因为插入/删除在一个文件的中间是可笑的昂贵;没有一个理智的设计会需要这个。


I need to Erase the file contents from a selected Point (C++ fstream) which function should i use ?

i have written objects , i need to delete these objects in middle of the file

解决方案

C++ has no standard mechanism to truncate a file at a given point. You either have to recreate the file (open with ios::trunc and write the contents you want to keep) or use OS-specific API calls (SetEndOfFile on Windows, truncate or ftruncate on Unix).

EDIT: Deleting stuff in the middle of a file is an exceedingly precarious business. Long before considering any other alternatives, I would try to use a server-less database engine like SQLite to store serialised objects. Better still, I would use SQLite as intended by storing the data needed by those objects in a proper schema.

EDIT 2: If the problem statement requires raw file access...

As a general rule, you don't delete data from the middle of a file. If the objects can be serialised to a fixed size on disk, you can work with them as records, and rather than trying to delete data, you use a table that indexes records within the file. E.g., if you write four records in sequence, the table will hold [0, 1, 2, 3]. In order to delete the second record, you simply remove its entry from the table: [0, 2, 3]. There are at least two ways to reuse the holes left behind by the table:

  1. On each insertion, scan for the first unused index and write the object out at the corresponding record location. This will become more expensive, though, as the file grows.
  2. Maintain a free list. Store, as a separate variable, the index of the most recently freed record. In the space occupied by that record encode the index of the record freed before it, and so on. This maintains a handy linked-list of free records while only requiring space fo one additional number. It is more complicated to work with, however, and requires an extra disk I/O when deleting and inserting.

If the objects can't be serialised to a fixed-length, then this approach becomes much, much harder. Variable-length record management code is very complex.

Finally, if the problem statement requires keeping records in order on disk, then it's a stupid problem statement, because insertion/removal in the middle of a file is ridiculously expensive; no sane design would require this.

这篇关于C ++ fstream从选定的点擦除文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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