C ++ fstream从选定的点擦除文件内容 [英] C++ fstream Erase the file contents from a selected Point
问题描述
我需要从选定的点(C ++ fstream)擦除文件内容,我应该使用哪个函数? code> objects ,我需要在文件中间删除这些 C ++没有标准的机制来截断给定点上的文件。你要么重新创建文件(用 编辑:删除文件中间的东西是一个非常危险的业务。在考虑其他选择之前,我会尝试使用像SQLite这样的无服务器数据库引擎来存储序列化的对象。更好的是,我将按照预期使用SQLite,将这些对象所需的数据存储在适当的模式中。 编辑2:声明需要原始文件访问... 一般来说,不要从文件中间删除数据。如果可以将对象序列化为磁盘上的固定大小,则可以将它们作为记录进行处理,而不是试图删除数据,而是使用一个将文件中的记录编入索引的表。例如,如果你按顺序写四条记录,表格将保存 如果对象可以不会被序列化为一个固定的长度,那么这种方法变得非常困难得多。变长记录管理代码是非常复杂的。 最后,如果问题陈述需要在磁盘上保持记录顺序,那么这是一个愚蠢的问题陈述,因为插入/删除在一个文件的中间是可笑的昂贵;没有一个理智的设计会需要这个。 I need to Erase the file contents from a selected Point (C++ fstream) which function should i use ? i have written C++ has no standard mechanism to truncate a file at a given point. You either have to recreate the file (open with 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 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屋!
objects
ios :: trunc
打开并写入你想保留的内容)或者使用特定于OS的API调用( Unix上的SetEndOfFile
,Unix上的 truncate
或 ftruncate
)。
$ b
[0,1,2,3]
。为了删除第二条记录,只需从表中删除条目: [0,2,3]
。至少有两种方式可以重复使用表中留下的空位:
objects
, i need to delete these objects
in middle of the fileios::trunc
and write the contents you want to keep) or use OS-specific API calls (SetEndOfFile
on Windows, truncate
or ftruncate
on Unix).[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: